Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...

like image 368
Minar Mahmud Avatar asked Feb 12 '15 12:02

Minar Mahmud


People also ask

Where does system out Println go?

println does not print to the console, it prints to the standard output stream ( System. out is Java's name of the standard output stream). The standard output stream is usually the console, but it doesn't have to be. The Java runtime just wraps the standard output stream of the operating system in a nice Java object.

Where does system out write data to?

System. out is used to print data to the output stream and there are no methods to read data. The output stream can be redirected to any destination such as file and the output will remain the same.

What does system out Println do in Java?

out. println(): This method prints the text on the console and the cursor remains at the start of the next line at the console. The next printing takes place from the next line.


2 Answers

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.
like image 111
Jon Skeet Avatar answered Sep 18 '22 22:09

Jon Skeet


The output is omitted, as long as you do not run your application from a console window (java -jar executable.jar). Furthermore, you can configure your Java installation such, that a console windows is launched as soon as you start a Java application. Output will be written to the JVM's console window then. You can find an article How do I enable and view the Java Console? on the official Java web site.

Activate Java console

like image 44
user1438038 Avatar answered Sep 19 '22 22:09

user1438038