Java无法执行系统命令(Ubuntu) [英] Java cannot execute system command (Ubuntu)

查看:419
本文介绍了Java无法执行系统命令(Ubuntu)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是我第一次尝试从Java执行系统命令;但这一次结果证明是非常困难的。我有一个脚本,从终端执行得很好。它从文件(input.txt)读取输入,它处理它并将结果导出到另一个文件(ouput.txt)。整件事不超过1秒。但是,当我尝试从Java执行它时,它会卡住并且永远不会完成。这是我的代码:

It's not the first time I have tried to execute a system command from Java; but this time it turns out to be very hard. I have a script that executes just fine from the terminal. It reads input from a file (input.txt), it processes it and exports the result in another file (ouput.txt). The whole thing lasts no more than 1sec. But, when I try to execute it from Java, it gets stuck and never finishes. This is my code:

Process p = new ProcessBuilder("./runCalculator.sh").start();
p.waitFor();

我也试过 Runtime.getRuntime()。exec(。 /runCalculator.sh)但都是一样的。我已经读过进程的InputStream和ErrorStream。错误流只返回开始计算...之类的消息。

I have also tried with Runtime.getRuntime().exec("./runCalculator.sh") but all the same. I've read both the InputStream and the ErrorStream of the process. The error stream returns nothing but a message like "Starting Calculation..."

任何想法?

推荐答案

您需要使用以下代码:

ProcessBuilder pb = new ProcessBuilder();
pb.command("bash", "-c", "./runCalculator.sh");
Process process = pb.start();
int retValue = process.waitFor();

这篇关于Java无法执行系统命令(Ubuntu)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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