Question

The following works fine when i type it in directly into cmd.exe:

netsh wlan connect name="Profile Name" ssid=XXXXXX

However when i try to do this from java it does not work, neither does it throw any exception. It is silently ignored:

Runtime.getRuntime().exec("cmd netsh wlan connect name=\"Profile Name\" ssid=XXXXX ") ; ` 

How can i improve the code ?

Was it helpful?

Solution

First try removing the cmd parameter (you don't need to run this interpreter, just netsh).

Else it may be due to whitespace characters in this command line (be careful of whitespace in SSID for example). You may want to try Runtime.exec(String[] cmdarray) or java.lang.ProcessBuilder instead to specify each parameter individually.

Examples:

Runtime.getRuntime().exec(new String[] {"netsh", "wlan", "connect", "name=\"Profile Name\"", "ssid=XXXXX"});

or (complete example):

ProcessBuilder pb = new ProcessBuilder("netsh", "wlan", "connect",
    "name=\"Profile Name\"", "ssid=XXXXX");
pb.redirectErrorStream(true);
Process process = pb.start();
BufferedReader reader = new BufferedReader(
    new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
    System.out.println(line);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top