Java: Can't execute external exe with arguments

前端 未结 3 615
Happy的楠姐
Happy的楠姐 2021-01-21 17:05

I’m trying to run an external program with arguments. The program can take different types of arguments, for instance avl tip.avl or avl < test.ops<

相关标签:
3条回答
  • 2021-01-21 17:46

    Thank you both, the below works as expected.

    File pipe = new File("test_0.ops");
    ProcessBuilder pb = new ProcessBuilder("avl");
    pb.redirectInput(pipe);
    final Process p = pb.start();
    
    0 讨论(0)
  • 2021-01-21 17:58

    the "<" does not get sent as an argument, but as an input after the program runs

    No, that’s not right. It is passed as an argument, just as test_0.avl and test_0.ops are.

    […] the same as just running avl and then typing tip.avl

    No, that is never what happens in the shell. The shell will pass tip.avl as the first argument.

    That said, it is your shell that special-handles the < sign, as it would special-handle > and |. When you use ProcessBuilder, that special handling will not happen. Your second invocation is equivalent to this in the shell:

    avl '<' test_0.ops
    

    This will disable the sepcial meaning of the <. That’s not what you wanted here, of course.

    0 讨论(0)
  • 2021-01-21 18:00

    You cannot redirect input like that from Java. Using < is a special shell pipe redirection command.

    You will either have to use processBuilder.getOutputStream() to write data to the process, or else you can use redirectInput.

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