What happens to "System.out.println()" in executable jar?

Minar Mahmud picture Minar Mahmud · Feb 12, 2015 · Viewed 18.4k times · Source

Suppose I've created an executable jar from a code where I have used

System.out.println()

When we run the executable jar, there is no console. So, what happens to this line? How does java handle this situation?

EDIT 01:

NOTE: The situation is when I don't use a console to run the jar nor associate any console with it anyhow.

EDIT 02: Making things clearer:

I know that nothing will be printed anywhere as there is no console..! I want to know how java handle this line in this case? Is this line omitted when generating the bytecode for a executable jar? Or is this line just overlooked when there is no console? Or anything...

Answer

Jon Skeet picture Jon Skeet · Feb 12, 2015

There's nothing special about running code in an executable jar file. If you run it from a console, e.g. with java -jar foo.jar the output will still go to the console.

If you run the code in some way that doesn't attach a console - such as javaw on Windows, which is the default program associated with executable jar files - then the output won't go anywhere. It won't cause any errors - the text will just be lost.

Note that in that case, if you use System.console() instead, that will return null. So:

System.out.printf("Foo%n"); // No problem. Goes nowhere.
System.console().printf("Foo%n"); // Would throw a NullPointerException.