运行时执行程序意外停止
我有一个用 C 语言编写的小可执行程序,它可以向文件生成大量输出。 当我使用运行时调用该程序时,如下所示:
Runtime r = Runtime.getRuntime();
Process p = null;
p = r.exec("./my_program -in input.file -out output.file", null, new File(System.getProperty("java.io.tmpdir")));
当程序产生低输出时,一切正常,但是当我使用大输入调用“*my_program*”时,它将在output.file中产生大量输出,但在在这种情况下,我的 Java 程序冻结,没有任何反应...
我在终端中使用大量大输入测试“*my_program*”,一切正常,但是当我使用 Runtime.exec 调用 Java 中的程序时,Java 程序冻结。
-- 提前致谢
I have a little executable program in C that produce a lot of output to a file.
When I call this program with Runtime, like this:
Runtime r = Runtime.getRuntime();
Process p = null;
p = r.exec("./my_program -in input.file -out output.file", null, new File(System.getProperty("java.io.tmpdir")));
When the program produce low output everything is ok, but when I call "*my_program*" with a large input it will produce a large quantity of output to the output.file, but in this case my program in Java freeze and nothing happen...
I test "*my_program*" in terminal with a lot of large inputs and everything is ok, but when I call the program in Java with Runtime.exec, the Java program freeze.
--
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保您正在读取 Process 的
.getOutputStream()
和.getErrorStream()
(如果尚未读取)。查看您的代码片段,您似乎只是在执行.exec(...)
(并且可能正在等待它完成,并且未显示对.waitFor()< 的调用) /代码>?)。
根据 http://download.oracle.com/javase/ 6/docs/api/java/lang/Process.html(强调):
Make sure you're reading from the Process's
.getOutputStream()
and.getErrorStream()
if you aren't already. Looking at your code snippet, it appears that you're just executing.exec(...)
(and maybe waiting for it to complete with a call not shown to.waitFor()
?).Per http://download.oracle.com/javase/6/docs/api/java/lang/Process.html (emphasis added):