views:

115

answers:

6

I have a Java GUI-based application that writes some diagnostic messages to System.out and System.err. Where are these messages output when running on Windows? (For example, on Mac OS X, they're printed to the system console log.)

Edit

I should add that the Java application is packaged as a .exe, so (right now) I can't launch it using java. (I can copy the individual .JAR files to the Windows test machine, I suppose.)

Also, it's an app I inherited that didn't use a logging framework before; I'd like to modify it to use one, but I was hoping to quickly get some log output to diagnose a problem right now.

+1  A: 

System.err messages are printed on the console, if you run your class via command line. if you don't, you won't see those messages.

cd1
+2  A: 

AFAIK if you start your java application using java.exe, they are written to the console. If you start it using javaw.exe they go nowhere, since standard in/out/err are not redirected for GUI applications. (You could manually create a subprocess for javaw.exe and redirect the output, but that's out of scope, I suppose)

I would suggest to use some logging framework instead and write the messages to a log file. log4j or java.util.logging would be a good start ...

MartinStettner
+2  A: 

It really depends on how you launch your application. For example, if you were to launch your GUI from a windows command-line prompt (e.g. such as java -jar myApp.jar), then as you might expect both System.out and System.err would go to the console. If you're launching via the Windows shell itself (double-clicking on an executable JAR, for example) then these error streams are lost, to the best of my knowledge. Setting up execution of the app as a service (however unlikely it may sound in your situation) would also lose the output streams.

Use of the standard in put and output streams is not typically a good idea unless you're expecting your program to be called in a shell-script type environment where these are common channels for communication. As you've discovered yourself, these are typically not well defined in a GUI environment. If you really want to capture textual information while the program is running, consider using a "real" logging framework (such as log4j or the java.util.logging package) to capture messages to a log.

Andrzej Doyle
I updated my question to address these issues.
mipadi
All good answers, but I think this is the most in-depth. I also decided to bump up the plan to use a logging framework, so I think this was the best advice.
mipadi
+1  A: 

GUI-based applications, started with javaw when double-clicking on the .jar file, won't output the System.out and System.err:

http://stackoverflow.com/questions/805701/load-java-util-logging-config-file-for-default-initialization

"javaw" swallows all output to System.out and System.err. Try "java" instead (without "w").

Pindatjuh
+3  A: 

You can actually change where System.out and System.err point in your application usingsetout() and setErr().

So for exmaple, if you want to dump to a file you could do

System.setOut(new PrintStream("output.txt"));
System.setErr(new PrintStream("err.txt"));

Just do that once in main(String[] args) and you should be good to go.

Chad Okere
+1 I think this really fits @mipadi current need.
bryantsai
This is a good idea. I considered it, but I ended up deciding to use an actual logging framework. I was planning to do that anyway...just not *right now*, but alas. Anyway, thanks for the also good answer!
mipadi
A: 

You could use the Java console if it were ran with java.

mangoDrunk