Having spaces in Runtime.getRuntime().exec with 2 executables

后端 未结 1 1836
你的背包
你的背包 2020-12-02 01:36

I have a command that I need to run in java along these lines:

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


        
相关标签:
1条回答
  • 2020-12-02 02:25

    Each argument you pass to the command should be a separate String element.

    So you command array should look more like...

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

    Each element will now appear as a individual element in the programs args variable

    I would also, greatly, encourage you to use ProcessBuilder instead, as it is easier to configure and doesn't require you to wrap some commands in "\"...\""

    0 讨论(0)
提交回复
热议问题