ProcessBuilder adds extra quotes to command line

前端 未结 3 1799
执笔经年
执笔经年 2020-12-07 01:00

I need to build the following command using ProcessBuilder:

\"C:\\Program Files\\USBDeview\\USBDeview.exe\" /enable          


        
3条回答
  •  醉梦人生
    2020-12-07 01:46

    Joachim is correct, but his answer is insufficient when your process expects unified arguments as below:

    myProcess.exe /myParameter="my value"
    

    As seen by stefan, ProcessBuilder will see spaces in your argument and wrap it in quotes, like this:

    myProcess.exe "/myParameter="my value""
    

    Breaking up the parameter values as Joachim recommends will result in a space between /myparameter= and "my value", which will not work for this type of parameter:

    myProcess.exe /myParameter= "my value"
    

    According to Sun, in their infinite wisdom, it is not a bug and double quotes can be escaped to achieve the desired behavior.

    So to finally answer stefan's question, this is an alternative that SHOULD work, if the process you are calling does things correctly:

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

    This should give you the command "C:\Program Files\USBDeview\USBDeview.exe" "/enable \"My USB Device\"", which may do the trick; YMMV.

提交回复
热议问题