0

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 ?

xav
  • 5,452
  • 7
  • 48
  • 57
Andrei Vasilev
  • 597
  • 5
  • 18
  • 37
  • Read (and implement) *all* the recommendations of [When Runtime.exec() won't](http://www.javaworld.com/jw-12-2000/jw-1229-traps.html). That might solve the problem. If not, it should provide more information as to the reason it failed. Then ignore that it refers to `exec` and build the `Process` using a `ProcessBuilder`. Also break a `String arg` into `String[] args` to account for arguments which themselves contain spaces. – Andrew Thompson Apr 11 '14 at 05:03

1 Answers1

3

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);
}
xav
  • 5,452
  • 7
  • 48
  • 57