This week's book giveaway is in the Java 9 forum.
We're giving away four copies of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications and have Sander Mak & Paul Bakker on-line!
See this thread for details.
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Read terminal output.  RSS feed

 
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
Posts: 21116
85
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
Boost this thread!