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...
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With