0

I need to process a command in cmd and the command looks like this:

"c:\Program Files (x86)\HMA! Pro VPN\bin\HMA! Pro VPN.exe" -changeip

But I can't really add the " because I will get errors.. Is there a way to do that? What i've tried causes errors:

cmd.exec(""c:/Program Files (x86)/HMA! Pro VPN/bin/HMA! Pro VPN.exe" -reconnect");

How can I escape that character so it works?

Exception in thread "Thread-1" java.lang.IllegalArgumentException: Executable name has embedded quote, split the arguments
    at java.lang.ProcessImpl.isQuoted(Unknown Source)
    at java.lang.ProcessImpl.getExecutablePath(Unknown Source)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at HMACommand.reconnect(HMACommand.java:15)
Artemkller545
  • 979
  • 3
  • 21
  • 55

2 Answers2

1

You can escape characters using a backslash (\).

In your case the result will be this:

String test = "\"c:\\Program Files (x86)\\HMA! Pro VPN\\bin\\HMA! Pro VPN.exe\" -changeip";

You'll also have to escape the backslashes themselves.

Referring to your edit:

This answer explains why you get the error you're getting.

From the cited source:

On Windows platform, the decoding of command strings specified to Runtime.exec(String), Runtime.exec(String,String[]) and Runtime.exec(String,String[],File) methods, has been improved to follow the specification more closely. This may cause problems for applications that are using one or more of these methods with commands that contain spaces in the program name, or are invoking these methods with commands that are not quoted correctly.

Instead, use a ProcessBuilder.

Community
  • 1
  • 1
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
  • Read my edit please, I've got a few errors upon processing that command. – Artemkller545 Jan 07 '14 at 20:37
  • This is the answer I'd use. FYI you can also do "/c/Some directory/some stuff/" for cross platform support. – Brant Unger Jan 07 '14 at 20:37
  • @JeroenVannevel I see, but in the answer you sent me, I don't really understand why he created a map and then had the values "var1" and "Value1" if he already entered the command parameters inside ProcessBuilder? – Artemkller545 Jan 07 '14 at 20:47
  • @user3123545: I haven't got any experience with it myself, but as far as I can tell it is optional since this seems to indicate it automatically gets the values as defined in the `ProcessBuilder` constructor: `an environment, which is a system-dependent mapping from variables to values. The initial value is a copy of the environment of the current process (see System.getenv())`. – Jeroen Vannevel Jan 07 '14 at 20:53
0

You could use a ProcessBuilder like so

String[] cmdLine =  {
        "c:/Program Files (x86)/HMA! Pro VPN/bin/HMA! Pro VPN.exe",
        "-changeip"
};
try {
    ProcessBuilder pb = new ProcessBuilder(cmdLine);
    Process p = pb.start();
    p.waitFor();
} catch (IOException e) {
    e.printStackTrace();
} catch (InterruptedException e) {
    e.printStackTrace();
}   
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249