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.
Fortunately, there is a way to run a command containing pipes. The command must be prefixed with
cmd /C
. e.g.:Notes
IBM850
encoding. See Default character encoding for java console output.useDelimiter("\\A")
.