以下语句在CMD中键入时工作正常
wmic /node:IP /user:Someuser /password:PWD Process Call Create "cmd /c echo 'Required_Info' %SystemDrive% %SystemRoot%"
我试图通过java进程构建器来执行相同的操作。这是代码。
//some code
String wmic_cmd = "Process Call Create";
String wmic2 = " \"cmd /c echo 'Required_Info' %SystemDrive% %SystemRoot% \"";
cmdTokens.add(wmic_cmd);
cmdTokens.add(wmic2);
/*debug and checked the value of cmdTokens array - it was
[wmic, /node:10.0.0.0, /user:Someuser, /password:PWD, Process Call Create, "cmd /c echo 'Required_Info' %SystemDrive% %SystemRoot% "]
*/
ProcessBuilder pb = new ProcessBuilder(cmdTokens.toArray(new String[]{}));
Process proc = pb.start();
当我检查proc的错误流时,我得到以下内容 -
Process Call Create - Alias not found.
这表示WMIC的语法错误。因此,在将命令字符串数组发送到进程构建器时出现问题。
任何想法??
答案 0 :(得分:2)
根据您的命令行和您想要做的事情,这不是如何做到的。使用:
final List<String> cmd = Arrays.asList("wmic", "/node:IP", "/user:Someuser",
"/password:PWD", "Process", "Call", "Create",
"cmd /c echo 'Required_Info' %SystemDrive% %SystemRoot%"
)
// build processBuilder using cmd.toArray(new String[cmd.size()]) as a command
cmd /c echo etc
周围的引号用于阻止命令解释程序分隔参数。传递原始参数时无需复制它们,这就是您对ProcessBuilder
使用的内容。