Java:无法使用参数执行外部 exe [英] Java: Can't execute external exe with arguments

查看:30
本文介绍了Java:无法使用参数执行外部 exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行带有参数的外部程序.该程序可以采用不同类型的参数,例如avl tip.avlavl

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

我可以运行avl tip.avl

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

但是当我尝试

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

"<" 不会作为参数发送,而是作为程序运行后的输入发送.<代码>avl 在我从命令行尝试时工作正常,但无法通过 ProcessBuilder 使其工作.

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.

我认为 avl tip.avl 有效,因为运行 avl tip.avl 与运行 avl 然后输入 是一样的提示.avl.这就是 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 ...

我认为参数会一次传递.执行命令提示符输入所做的操作的正确方法是什么 avl <;test.ops + ENTER

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

推荐答案

您不能像从 Java 那样重定向输入.使用 < 是一个特殊的 shell 管道重定向命令.

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

您要么必须使用 processBuilder.getOutputStream() 将数据写入进程,否则您可以使用 redirectInput.

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

这篇关于Java:无法使用参数执行外部 exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆