pwa*_*t01 12 java process runtime.exec java-7
我有一个命令,我需要在这些行中使用java运行:
C:\path\that has\spaces\plink -arg1 foo -arg2 bar "path/on/remote/machine/iperf -arg3 hello -arg4 world"
Run Code Online (Sandbox Code Playgroud)
当路径没有空格时,此命令工作正常,但是当我有空格时,我似乎无法使其工作.我尝试过以下操作,运行Java 1.7
String[] a = "C:\path\that has\spaces\plink", "-arg1 foo", "-arg2 bar", "path/on/remote/machine/iperf -arg3 hello -arg4 world"
Runtime.getRuntime().exec(a);
Run Code Online (Sandbox Code Playgroud)
以及
String[] a = "C:\path\that has\spaces\plink", "-arg1 foo", "-arg2 bar", "path/on/remote/machine/iperf", "-arg3 hello", "-arg4 world"
Runtime.getRuntime().exec(a);
Run Code Online (Sandbox Code Playgroud)
但似乎都没有做任何事情.我有什么问题吗?
Mad*_*mer 17
传递给命令的每个参数都应该是一个单独的String元素.
所以你命令数组看起来应该更像......
String[] a = new String[] {
"C:\path\that has\spaces\plink",
"-arg1",
"foo",
"-arg2",
"bar",
"path/on/remote/machine/iperf -arg3 hello -arg4 world"};
Run Code Online (Sandbox Code Playgroud)
现在,每个元素都将作为program args变量中的单个元素出现
我也非常鼓励你使用ProcessBuilder,因为它更容易配置,并且不需要你包装一些命令"\"...\""