Java: Can't execute external exe with arguments

时光毁灭记忆、已成空白 提交于 2020-01-30 07:56: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

I can get avl tip.avl running through

try {
    String[] list = {"avl", "test_0.avl"};
    ProcessBuilder pb = new ProcessBuilder(list);
    pb.command(list);
    final Process p = pb.start();
    BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
} catch (Exception ex) {
    System.out.println(ex);
}

but when I try

String[] list = {"avl", "<", "test_0.ops"};
ProcessBuilder pb = new ProcessBuilder(list);
pb.command(list);
final Process p = pb.start();

the "<" does not get sent as an argument, but as an input after the program runs. avl < test.ops works ok when I try it from command line, but cant get it to work through ProcessBuilder.

I think avl tip.avl works because running avl tip.avl is the same as just running avl and then typing tip.avl. Which is what ProcessBuilder seems to be doing actually ...

I assumed the arguments would be passed all at one. What would be the right way of doing what the command prompt input does avl < test.ops + ENTER


回答1:


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.




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/49699647/java-cant-execute-external-exe-with-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!