Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Nigel Wade 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: References: <163b0e03-2224-427a-910a-e9ef41df36cf@googlegroups.com> <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 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.