File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Redirect output from stderr, stdout to a JTextArea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Redirect output from stderr, stdout to a JTextArea" Watch "Redirect output from stderr, stdout to a JTextArea" New topic
Author

Redirect output from stderr, stdout to a JTextArea

Konstantinos Vasileiou
Greenhorn

Joined: Jul 20, 2009
Posts: 16
I have implemented a program that invokes some Linux commands to complete a number of tasks. My problem is that I want to add a GUI to the application, so I need to somehow redirect the output of the commands from the error and the out streams to a graphical components, such as a JTextArea.... However, I am not sure how to do it. The code I currently have looks like this:


Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2156
    
    7
The [Message Console might be what you are looking for.
Ramses Butarbutar
Ranch Hand

Joined: Jun 16, 2009
Posts: 52
Hi Konstantinos ,

If i'm not mistaken, you wanna get your error (your stack trace info???) and put in some components such a JTextArea ?

Just define the exception, catch it, put it in stringwriter and parse into string.
After that you can easily throw it into your JTextArea.



Have a nice try..


At the center of non-violence stands the principle of love
.... Martin Luther King, Jr.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Rob Camick wrote:The [Message Console might be what you are looking for.

It would be great if it was the stderr and stdout of the current application that should be great. In this case, it's the stderr and stdout from a separate process.

Konstantinos, first of all, please read this. You'll find it very useful.
In that article, the StreamGobbler code on page 4 is almost what you want. However, instead of printing, you want to add it:
Apart from appending only the exception's String form, you can print the stack trace to the text area as well, using my patented ( ) TextAreaWriter:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Konstantinos Vasileiou
Greenhorn

Joined: Jul 20, 2009
Posts: 16
Thank you - very helpful and detailed reply!
 
jQuery in Action, 2nd edition
 
subject: Redirect output from stderr, stdout to a JTextArea
 
Similar Threads
execute ftp commands using java API
how to execute ftp commands from java
runnning a shell script from mac os x
Exceute an exe file
problem running sqlplus using Runtime.exec()