Search code examples
javawindowsprocessprocessbuilder

ProcessBuilder adds extra quotes to command line


I need to build the following command using ProcessBuilder:

"C:\Program Files\USBDeview\USBDeview.exe" /enable "My USB Device"

I tried with the following code:

ArrayList<String> test = new ArrayList<String>();
test.add("\"C:\\Program Files\\USBDeview\\USBDeview.exe\"");
test.add("/enable \"My USB Device\"");  

ProcessBuilder processBuilder = new ProcessBuilder(test);                       
processBuilder.start().waitFor();   

However, this passes the following to the system (verified using Sysinternals Process Monitor)

"C:\Program Files\USBDeview\USBDeview.exe" "/enable "My USB Device""

Note the quote before /enable and the two quotes after Device. I need to get rid of those extra quotes because they make the invocation fail. Does anyone know how to do this?


Solution

  • As far as I understand, since ProcessBuilder has no idea how parameters are to be passed to the command, you'll need to pass the parameters separately to ProcessBuilder;

    ArrayList<String> test = new ArrayList<String>();
    test.add("\"C:\\Program Files\\USBDeview\\USBDeview.exe\"");
    test.add("/enable");
    test.add("\"My USB Device\"");