"System.out.println()"会如何处理?在可执行jar中? [英] What happens to "System.out.println()" in executable jar?
问题描述
假设我已经从使用过的代码中创建了executable jar
Suppose I've created an executable jar
from a code where I have used
System.out.println()
当我们运行可执行jar时,没有控制台.那么,这条线怎么办? java
如何处理这种情况?
When we run the executable jar, there is no console. So, what happens to this line? How does java
handle this situation?
编辑01 :
注意:这种情况是当我不使用控制台来运行jar
或将其与任何控制台相关联时.
NOTE: The situation is when I don't use a console to run the jar
nor associate any console with it anyhow.
EDIT 02 :使事情更清晰:
我知道什么都不会打印,因为没有控制台..!我想知道在这种情况下java
如何处理这一行?生成可执行jar的字节码时,是否会省略此行?还是在没有控制台的情况下忽略了这条线?还是什么...
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...
推荐答案
在可执行jar文件中运行代码没有什么特别之处.如果您是从控制台运行的,例如使用java -jar foo.jar
,输出仍将转到控制台.
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.
如果您以某种不附加控制台的方式运行代码(例如Windows上的javaw
,这是与可执行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.
请注意,在这种情况下,如果改用System.console()
,则 that 将返回null
.所以:
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.
这篇关于"System.out.println()"会如何处理?在可执行jar中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!