在Runtime.getRuntime()。exec中有2个可执行文件的空格

我有一个命令,我需要在这些行中使用java运行:

C:\path\that has\spaces\plink -arg1 foo -arg2 bar "path/on/remote/machine/iperf -arg3 hello -arg4 world" 

当路径没有空格时,此命令工作正常,但是当我有空格时,我似乎无法使其工作。 我尝试过以下操作,运行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); 

以及

 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); 

但似乎都没有做任何事情。 我有什么问题吗?

传递给命令的每个参数都应该是一个单独的String元素。

所以你命令数组看起来应该更像……

 String[] a = new String[] { "C:\path\that has\spaces\plink", "-arg1", "foo", "-arg2", "bar", "path/on/remote/machine/iperf -arg3 hello -arg4 world"}; 

现在,每个元素都将显示为程序args变量中的单个元素

我也非常鼓励您使用ProcessBuilder ,因为它更容易配置,并且不需要您在"\"...\""包含一些命令"\"...\""