main() 方法的 Java 应用程序退出代码究竟是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28738307/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:42:54  来源:igfitidea点击:

How exactly works the Java application exit code of the main() method?

javamainexit-code

提问by AndreaNobili

I have the following doubts related a simple command line Java application.

我对一个简单的命令行 Java 应用程序有以下疑问。

So I have this command line application that is started by a main()method defined inside a Mainclass. As usual this main()method is defined with this signature:

所以我有这个命令行应用程序,它由main()Main类中定义的方法启动。像往常一样,这个main()方法是用这个签名定义的:

public static void main(String[] args) {

It's return type is void, and that should mean it doesn't return any value. But when its execution correctly terminates I obtain following message in the IntelliJ console.

它的返回类型是void,这应该意味着它不返回任何值。但是当它的执行正确终止时,我会在 IntelliJ 控制台中获得以下消息。

Disconnected from the target VM, address: '127.0.0.1:54090', transport: 'socket'

Process finished with exit code 0

What exactly does represent the exit code 0? I think it means that the program have correctly completed its execution without incur into any error.

究竟代表exit code 0什么?我认为这意味着该程序已正确完成其执行而不会产生任何错误。

So now I have the following 2 doubts:

所以现在我有以下2个疑问:

  1. If it is true why it happens if my main()method return void?

  2. How can I return a different exit code if my application ended with an error?

  1. 如果这是真的,为什么我的main()方法返回会发生这种情况void

  2. 如果我的应用程序以错误结束,我如何返回不同的退出代码?

Is there a standard exit code value for ending with errors?

是否有以错误结尾的标准退出代码值?

采纳答案by JB Nizet

The VM exits when

虚拟机退出时

  • all of the non-daemon threads stop running, or
  • System.exit(exitCode)is called
  • 所有非守护线程停止运行,或
  • System.exit(exitCode)叫做

In the first case, the exit code is 0. In the second case, it's the exit code passed to the exit()method.

在第一种情况下,退出代码为 0。在第二种情况下,它是传递给exit()方法的退出代码。

Don't forget that even if your main() method returns, the program will continue running until no non-daemon thread runs anymore. And any thread running in the VM can choose to exit explicitely.

不要忘记,即使您的 main() 方法返回,程序也会继续运行,直到不再有非守护线程运行。并且在 VM 中运行的任何线程都可以选择显式退出。

The exit code 0 means that everything went as expected. you can choose to use any other exit code to signal an exceptional condition to the environment.

退出代码 0 表示一切都按预期进行。您可以选择使用任何其他退出代码来向环境发出异常情况信号。

回答by AlexR

Exit code of process is what process reports to operating system as its error code.

进程的退出代码是进程向操作系统报告的错误代码。

Java designers couldmake main()method to return intso that JVM could report to OS this value as a process exit code. But they decided to make main void but provide API that can update this code using System.exit(exitCode). The advantage of this solution is that program can decide to exit at any point and in any thread, not only in main method and in main thread.

Java 设计者可以使main()方法返回,int以便 JVM 可以将此值作为进程退出代码报告给操作系统。但是他们决定使 main 无效,但提供可以使用System.exit(exitCode). 这种解决方案的优点是程序可以决定在任何时间点和任何线程中退出,而不仅仅是在主方法和主线程中。

回答by NickJ

An exit code of 0 means it completed normally, that is standard for all processes, not just java. The value is not returning from the main method (it's void) but from the JVM itself.

退出代码 0 表示它正常完成,这是所有进程的标准,而不仅仅是 java。该值不是从主方法(它是空的)返回,而是从 JVM 本身返回。

A different value can be specified, e.g. System.exit(1)to indicate some error condition, and the program stops.

可以指定不同的值,例如System.exit(1)指示某些错误情况,然后程序停止。