java 可执行 jar 中的“System.out.println()”会发生什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28477529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What happens to "System.out.println()" in executable jar?
提问by Minar Mahmud
Suppose I've created an executable jar
from a code where I have used
假设我已经executable jar
从我使用过的代码中创建了一个
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?
当我们运行可执行 jar 时,没有控制台。那么,这条线会发生什么?java
这种情况如何处理?
EDIT 01:
编辑 01:
NOTE: The situation is when I don't use a console to run the jar
nor associate any console with it anyhow.
注意:这种情况是我不使用控制台来运行,jar
也不以任何方式将任何控制台与其关联。
EDIT 02: Making things clearer:
编辑 02:让事情更清楚:
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...
我知道任何地方都不会打印任何内容,因为没有控制台..!我想知道java
在这种情况下如何处理这条线?为可执行 jar 生成字节码时是否省略了这一行?或者当没有控制台时这条线只是被忽略了?或者什么...
采纳答案by Jon Skeet
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.
在可执行 jar 文件中运行代码没有什么特别之处。如果您从控制台运行它,例如java -jar foo.jar
输出仍将转到控制台。
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.
如果您以某种不附加控制台的方式运行代码——例如javaw
在 Windows 上,这是与可执行 jar 文件关联的默认程序——那么输出将不会去任何地方。它不会导致任何错误 - 文本只会丢失。
Note that in that case, if you use System.console()
instead, thatwill return null
. So:
请注意,在这种情况下,如果你使用的System.console()
不是,那将返回null
。所以:
System.out.printf("Foo%n"); // No problem. Goes nowhere.
System.console().printf("Foo%n"); // Would throw a NullPointerException.
回答by user1438038
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.
只要您不从控制台窗口 ( java -jar executable.jar
)运行您的应用程序,就会省略输出。此外,您可以配置 Java 安装,以便在启动 Java 应用程序后立即启动控制台窗口。然后输出将写入 JVM 的控制台窗口。您可以找到一篇文章如何启用和查看 Java 控制台?在官方 Java 网站上。
回答by DiscoStu
I you open it from the console (java -jar YourJar.jar) the text gets printed in your console window.
我从控制台 (java -jar YourJar.jar) 打开它,文本会打印在控制台窗口中。
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.
澄清你的第二个编辑:字节码没有被省略,因为编译器不知道 jar 将在什么上下文中执行。jar 总是可以从控制台调用,在这种情况下 println 必须留在那里。
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 notneccessary for a java-Program to have a GUI (or to run completely in the background).
事实上,否则许多 jar 文件将完全无用。有许多 Java 程序通过控制台输入和输出与用户交互。这是不是neccessary对于Java的程序有一个GUI(或在后台运行完全)。