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
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
IBM850
encoding. See Default character encoding for java console output.useDelimiter("\\A")
.