I’m working on a Java program, and it’s been over a year since the last time I used Java, so I’m a little rusty. This program uses Runtime.exec() to call other programs to do its dirty work, and needs to parse their output and update its own GUI accordingly in real time while the other programs are working. What’s the best way to do this? I’m currently thinking of having my external-program-executor class have its own internal thread that polls the external program’s output stream and then raises events when noteworthy things happen, and then implementing my own EventListener interface for my UI classes. I worry however how that will handle the asynchronous nature of the events being fired. Can anyone give any tips on how to protect the listeners from race conditions, and/or a better approach? Thanks.
I’m working on a Java program, and it’s been over a year since the
Share
Processobject returned fromRuntime.exec(String)has methods for getting theInputStreamfor both stderr and stdout and theOutputStreamfor stdin.OutputStream. Simply push your data on the stream.Threadthat waits on the stdoutOutputStream. Everytime there is new data to read, it will read the data and create an event.EventListenerthat listens for certain output events. These events are (possibly translated to a different format) onto theOutputStreamand can be read by the stdin of the external process.Good luck.