Java Runtime.getRunTime().exec(CMD) not su

2020-03-06 07:41发布

I'm attempting to write a program that will display and be able to update your IP address settings using a JFrame window. I am looking at running this purely on windows so I'm attempting to be able to use the netsh windows command to retrieve/set details.

The windows command: netsh interface ip show config name="Local Area Connection" | Find "IP" returns exactly what I want it to, however the code I have written will not work past the pipe, it will only work if I write up to the "Local Area Connection" part.

Is there any way of using the pipe feature to be able to return specifically just the IP address? I read that you can pass the line as a string array, ie String[] cmd = netsh........

package ipchanger;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class test {

    private String CMD;

public void executecommand(String CMD) {
        this.CMD = CMD;

        try {
            // Run whatever string we pass in as the command
            Process process = Runtime.getRuntime().exec(CMD);

            // Get input streams
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));

            // Read command standard output
            String s;
            System.out.println("Standard output: ");
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);

            }

            // Read command errors
            System.out.println("Standard error: ");
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
}


public test() {
    String FINDIP = "netsh interface ip show config name=\"Local Area Connection\" | Find \"IP\"";
    //System.out.println(FINDIP);
    executecommand(FINDIP);

}


public static void main(String[] args) {
    new test();
}
}

Thought you guys might be able to help.

1条回答
Explosion°爆炸
2楼-- · 2020-03-06 08:34

Fortunately, there is a way to run a command containing pipes. The command must be prefixed with cmd /C. e.g.:

public static void main(String[] args) throws Exception {
    String command = "cmd /C netstat -ano | find \"3306\"";
    Process process = Runtime.getRuntime().exec(command);
    process.waitFor();
    if (process.exitValue() == 0) {
        Scanner sc = new Scanner(process.getInputStream(), "IBM850");
        sc.useDelimiter("\\A");
        if (sc.hasNext()) {
            System.out.print(sc.next());
        }
        sc.close();
    } else {
        Scanner sc = new Scanner(process.getErrorStream(), "IBM850");
        sc.useDelimiter("\\A");
        if (sc.hasNext()) {
            System.err.print(sc.next());
        }
        sc.close();
    }
    process.destroy();
}

Notes

查看更多
登录 后发表回答