本文介绍了ProcessBuilder为命令行添加了额外的引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用ProcessBuilder构建以下命令:

I need to build the following command using ProcessBuilder:

"C:\Program Files\USBDeview\USBDeview.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 Files\USBDeview\USBDeview.exe" "/enable "My USB Device""

请注意 / enable 之前的报价和设备之后的两个引号。我需要摆脱那些额外的引号,因为它们使调用失败。有谁知道怎么做?

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为命令行添加了额外的引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 03:58