Java处理子进程 [英] Java Dealing with child Process

查看:164
本文介绍了Java处理子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的脚本,使用 ProcessBuilder 将输出写入控制台。并且 OutputStreamWriter 请参阅:

I have a simple script that writes output to the console using ProcessBuilder. And OutputStreamWriter see:

带输入/输出流的Java进程

早期的线程。我相信我的问题在于产生子进程并且初始的父进程被终止的事实。导致 OutputStreamWriter 抛出 java.io.IOException:Broken pipe exception 。鉴于应用程序正在生成第二个进程,如何将OutputStreamWriter连接到此新进程?包括能够读回它产生的输出吗?这在Java中甚至可能吗? Surly java应该能够遵循产生的过程。

An earlier thread. I believe my problem rests with the fact a sub process is spawned and the initial Parent process is killed. Resulting in the OutputStreamWriter to throw a java.io.IOException: Broken pipe exception. Given the application is spawning a second process how can I connect my OutputStreamWriter to this new process? Including being able to read back the output it is generating? Is this even possible in Java? Surly java should be able to follow onto the spawned process.

谢谢

推荐答案

您是否正在致电 waitFor()您正在运行的进程?这应该确保你的父母没有在孩子面前完成。您可能还想查看 commons exec ,这是一个旨在让您的生活充满活力的开源库在Java中运行单独的进程时更容易。

Are you calling waitFor() on the process you are running? That should ensure you're parent doesn't complete before the child. You may also want to look at commons exec, which is an open source library designed to make your life easier when running separate processes in Java.

这篇关于Java处理子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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