Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Read terminal output.

 
Charles Mulloy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to catch terminal feedback and send it to a JTextArea. Below is a method I use to send commands to the OS. When I use it, the terminal will show the output (as long as you have a terminal to start with, in jar form, you won't see the feedback). Anyone know how I can bend terminal feed back to my will?

If it matters here is the method I use to send commands to the OS. I found this method online. I bookmarked the pages but had to reformat. I'll post the link if I can find it again.
 
Rob Spoor
Sheriff
Pie
Posts: 20753
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to change only one line: Instead of printing it to System.out you need to append it to your JTextArea. While normally you need to be careful with threading in Swing (see http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html), JTextArea's append method is thread safe so you can simply append all characters. Use String.valueOf((char)c) to convert the character to a String.

That said, you may want to use the bulk reader methods in conjunction with a BufferedReader to provide a little more efficiency. Your code hardly changes:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic