ProcessBuilder 向命令行添加额外的引号 [英] ProcessBuilder adds extra quotes to command line
问题描述
我需要使用 ProcessBuilder 构建以下命令:
I need to build the following command using ProcessBuilder:
"C:Program FilesUSBDeviewUSBDeview.exe" /enable "My USB Device"
我尝试使用以下代码:
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();
但是,这会将以下内容传递给系统(使用 Sysinternals Process Monitor 进行验证)
However, this passes the following to the system (verified using Sysinternals Process Monitor)
"C:Program FilesUSBDeviewUSBDeview.exe" "/enable "My USB Device""
注意/enable
之前的引号和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?
推荐答案
据我所知,由于 ProcessBuilder 不知道如何将参数传递给命令,因此您需要将参数单独传递给 ProcessBuilder;
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"");
这篇关于ProcessBuilder 向命令行添加额外的引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!