Java Dealing with child Process

2020-08-04 03:40发布

问题:

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

Java Process with Input/Output Stream

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.

Thanks

回答1:

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 process