如何从程序中退出 Java 应用程序

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

How to quit a java app from within the program

java

提问by Olaseni

What's the best way to quit a Java application with code?

使用代码退出 Java 应用程序的最佳方法是什么?

采纳答案by Jon

You can use System.exit()for this purpose.

您可以System.exit()为此目的使用。

According to oracle's Java 8 documentation:

根据 oracle 的 Java 8 文档:

public static void exit(int status)

Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.

This method calls the exit method in class Runtime. This method never returns normally.

The call System.exit(n)is effectively equivalent to the call:

Runtime.getRuntime().exit(n)

public static void exit(int status)

终止当前运行的 Java 虚拟机。参数用作状态代码;按照惯例,非零状态代码表示异常终止

该方法调用 Runtime 类中的 exit 方法。此方法永远不会正常返回。

该调用System.exit(n)实际上等效于以下调用:

Runtime.getRuntime().exit(n)

回答by Chris Cooper

System.exit(0);

The "0" lets whomever called your program know that everything went OK. If, however, you are quitting due to an error, you should System.exit(1);, or with another non-zero number corresponding to the specific error.

“0”让任何调用您程序的人都知道一切正常。但是,如果您因错误而退出,您应该System.exit(1);,或者使用与特定错误相对应的另一个非零数字。

Also, as others have mentioned, clean up first! That involves closing files and other open resources.

另外,正如其他人所说,先清理!这涉及关闭文件和其他打开的资源。

回答by Freiheit

I agree with Jon, have your application react to something and call System.exit().

我同意 Jon,让您的应用程序对某些内容做出反应并调用 System.exit()。

Be sure that:

请确保:

  • you use the appropriate exit value. 0 is normal exit, anything else indicates there was an error
  • you close all input and output streams. Files, network connections, etc.
  • you log or print a reason for exiting especially if its because of an error
  • 您使用适当的退出值。0 是正常退出,其他任何表示有错误
  • 您关闭所有输入和输出流。文件、网络连接等。
  • 你记录或打印退出的原因,特别是如果它是因为错误

回答by Christian Semrau

System.exit()is usually not the best way, but it depends on your application.

System.exit()通常不是最好的方法,但这取决于您的应用程序。

The usual way of ending an application is by exiting the main()method. This does not work when there are other non-deamon threads running, as is usual for applications with a graphical user interface (AWT, Swing etc.). For these applications, you either find a way to end the GUI event loop (don't know if that is possible with the AWT or Swing), or invoke System.exit().

结束应用程序的常用方法是退出该main()方法。当有其他非守护线程正在运行时,这不起作用,这对于具有图形用户界面的应用程序(AWT、Swing 等)来说是常见的。对于这些应用程序,您要么找到一种方法来结束 GUI 事件循环(不知道 AWT 或 Swing 是否可行),要么调用System.exit().

回答by Bastien

System.exit(int i)is to be used, but I would include it inside a more generic shutdown()method, where you would include "cleanup" steps as well, closing socket connections, file descriptors, thenexiting with System.exit(x).

System.exit(int i)将被使用,但我会将它包含在一个更通用的shutdown()方法中,您还将在其中包含“清理”步骤,关闭套接字连接、文件描述符,然后System.exit(x).

回答by Vaishak Suresh

The answer is System.exit(), but not a good thing to do as this aborts the program. Any cleaning up, destroy that you intend to do will not happen.

答案是 System.exit(),但这不是一件好事,因为这会中止程序。任何你打算做的清理、破坏都不会发生。

回答by Joshua

Using dispose();is a very effective way for closing your programs.

使用dispose();是关闭程序的一种非常有效的方法。

I found that using System.exit(x)resets the interactions pane and supposing you need some of the information there it all disappears.

我发现使用会System.exit(x)重置交互窗格并假设您需要一些信息,所有信息都会消失。

回答by Dude Bro

Runtime.getCurrentRumtime().halt(0);

回答by pooploser0110

System.exit(ABORT); Quit's the process immediately.

System.exit(ABORT); 立即退出进程。

回答by Anton

There's two simple answers to the question.

这个问题有两个简单的答案。

This is the "Professional way":

这是“专业方式”:

//This just terminates the program.
System.exit(0);

This is a more clumsier way:

这是一种更笨拙的方法:

//This just terminates the program, just like System.exit(0).
return;