04 August 2005 - java_dev (original) (raw)
If you've ever wanted to include a Console in your Java application, you've probably googled "How to make a Java Console." Then you probably happened upon this page and thought awesome! This is exactly what I need! Right? Wrong.
Repast, an open source sim program has a console based on that Console. I'm writing an IDE for Repast (called IndigoSim), so we decided we needed a Console too.
Very quickly we noticed a bunch of problems. Some output was being printed out twice while some output was being lost. In some cases, stderr and stdout would do weird things and you'd get stdout output in the middle of an exception print out. If there was a lot of output, the program would slow down considerably. Finally, after opening up the RepastConsole, output would stop appearing on my actual OS X terminal.
I took an axe to RepastConsole, changed a bunch of things, and got a new Console which seems to work correctly. As an added bonus, it also copies output and error to the real console. It also works with Logging packages.
The files are here:
http://indigosim.sourceforge.net/files/IndigoConsole.java
http://indigosim.sourceforge.net/files/CopyPrintStream.java
They are part of a bigger project that is liscensed under the BDSL so feel free to use them. You might have to delete or change some lines to get it to work in your app.
If anyone's interested in exactly why the Console program above has so many problems, I'll explain it in my next post.