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

3

15

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

Wera answered 12/2, 2015 at 12:30 Comment(5)
"When we run the executable jar, there is no console" - well that depends on how you run it. If you run java -jar foo.jar from a console, then there is...Shigella
I mean running not using console...Wera
Then it's worth making your question clearer. You have stated that there is no console, when that's only sometimes true.Shigella
If you run the program not using the console, then you won't see System.out.print's contents. Think of it as something primarily used for testing/debugging. When you go to a production program this should not be used.Aaronson
If you actually want to use System.out.print() then you can redirect from the console to a file: #2851734Persiflage
S
11

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.
Shigella answered 12/2, 2015 at 12:36 Comment(2)
the text will just be lost. So can I assume that this println() method is written in a way that if there is no console, the call will just be ignored?Wera
@minarmahmud: Well not ignored - I suspect if you use printf and have some invalid format string, that will fail, for example. But the output won't go anywhere.Shigella
F
2

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

Fossette answered 12/2, 2015 at 12:37 Comment(0)
Y
1

I you open it from the console (java -jar YourJar.jar) the text gets printed in your console window.

If you open it in the explorer (or similar), you won't see the text

To clarify your second Edit: The bytecode is not omitted, because the compiler cannot know in what context the jar will be executed. The jar can always be called from console, in that case the println has to stay there.

In fact, many jar Files would be completely useless otherwise. There are plenty of Java programs that interact with the user by console in- and output. It is not neccessary for a java-Program to have a GUI (or to run completely in the background).

Ywis answered 12/2, 2015 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.