Groups | Search | Server Info | Keyboard shortcuts | Login | Register


Groups > comp.lang.java.gui > #5517

Re: how to direct console output to a textarea

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Nigel Wade <nmw@local.domain>
Newsgroups comp.lang.java.gui
Subject Re: how to direct console output to a textarea
Date Mon, 9 Jan 2017 12:11:59 +0000
Lines 33
Message-ID <edhd0fFfv5U1@mid.individual.net> (permalink)
References <163b0e03-2224-427a-910a-e9ef41df36cf@googlegroups.com> <ed6ls7FbfjkU1@mid.individual.net> <2dc6919f-33e8-44c7-86c9-ab0a7859ce0d@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net HeWlxNGCYLty/IWaxTTPiwnq7ZuP79YeakBnDCZbEQuDkLl8Yt
Cancel-Lock sha1:SUVXjpw/e6wvB30cgByz41qW9kM=
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0
In-Reply-To <2dc6919f-33e8-44c7-86c9-ab0a7859ce0d@googlegroups.com>
Xref csiph.com comp.lang.java.gui:5517

Show key headers only | View raw


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.

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

how to direct console output to a textarea abhprk3926@gmail.com - 2017-01-04 23:56 -0800
  Re: how to direct console output to a textarea Nigel Wade <nmw@local.domain> - 2017-01-05 10:35 +0000
    Re: how to direct console output to a textarea abhprk3926@gmail.com - 2017-01-05 03:04 -0800
      Re: how to direct console output to a textarea Nigel Wade <nmw@local.domain> - 2017-01-09 12:11 +0000
      Re: how to direct console output to a textarea kanisfatemashanta28@gmail.com - 2018-12-21 00:35 -0800
  Re: how to direct console output to a textarea Abhay Prakash <abhprk3926@gmail.com> - 2017-01-09 07:17 -0800
    Re: how to direct console output to a textarea kanisfatemashanta28@gmail.com - 2018-12-18 11:30 -0800

csiph-web