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

后端 未结 3 1058
刺人心
刺人心 2020-12-11 15:17

Suppose I\'ve created an executable jar from a code where I have used

System.out.println()

When we run the executable jar, th

3条回答
  •  遥遥无期
    2020-12-11 15:58

    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.
    

提交回复
热议问题