On 05/01/17 11:04,
abhprk3926@gmail.com wrote:
Can You Guide Me On As To How Exactly Read The Output Of the Process.
i cant seem to figure a way out to read from the output stream of the process
If you create a process using ProcessBuilder, ProcessBuilder.start returns a Process.
From the Process you get the standard output stream from Process.getInputStream, and
the standard error from Process.getErrorStream.
However, if the process is likely to produce output on both stdout and stderr then you
need to read both in parallel on different threads to avoid possible deadlock. If you are
ok with reading merged stdout and stderr then you can use ProcessBuilder.redirectErrorStream(true)
before invoking ProcessBuilder.start().
Since each sub-process generates its own stdout/stderr you have to do this for each process you create
in the same logical block which creates the sub-process. The code which reads stdout/stderr must be run
by a different thread from the one which waits for the process to complete, otherwise you risk deadlock.
Therefore you'll need to modify your code so that the blocks which create your sub-processes also read the output
from those processes, and since you want that output to be appended to a JComponent the output needs to be processed
on the EDT. You already have a model for the code which might do this using SwingWorker, but you need to figure out
how to get the Processes InputStream into the SwingWorker doInBackground method. Also, since it's an InputStream the easiest
way to read it is to wrap it in an InputStreamReader, which in turn is wrapped by a BufferedReader. Then you can read
using
BufferedReader out = new BufferedReader(new InputStreamReader( process.getInputStream());
while((line = out.readLine()) != null) {
publish(line);
}
Another thing which you need to correct is your ActionListeners which create the sub-processes. At the moment they will block the
EDT until the process is complete thus preventing the SwingWorkers from executing, and locking the GUI.
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)