Monday, March 30, 2015

Java: Getting console output of an executable .jar file on Windows

Problem:
I added a few System.out.println() statements in my executable .jar for logging purposes. It works fine in Eclipse. But when I run it through cmd, I noticed there's no console output at all.


What's happening?
By default, .jar files are handled by javaw.exe instead of java.exe on Windows.

"The javaw command is identical to java, except that with javaw there is no associated console window. Use javaw when you do not want a command prompt window to appear. The javaw launcher will, however, display a dialog box with error information if a launch fails."
http://docs.oracle.com/javase/8/docs/technotes/tools/windows/java.html

So this is by design.


Solution:
If you want to get the console output, there are 2 options.

#1. Manually call java.exe to execute your .jar file, e.g.
java -jar <your_jar_file_path>

- or -

#2. Change the .jar file association from javaw.exe to java.exe
a) Open regedit
b) Navigate to HKEY_CLASSES_ROOT\jarfile\shell\open\command
c) Change the path from javaw.exe to java.exe

No comments: