How to quit a java app from within the program

Java

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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

According to oracle's Java 8 documentation:

>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)

Solution 2 - Java

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.

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

Solution 3 - Java

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, then exiting with System.exit(x).

Solution 4 - Java

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

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().

Solution 5 - Java

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

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

Solution 6 - Java

I agree with Jon, have your application react to something and call 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

Solution 7 - Java

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.

Solution 8 - Java

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;



Solution 9 - Java

Runtime.getCurrentRumtime().halt(0);

Solution 10 - Java

System.exit() will do what you want. But in most situations, you probably want to exit a thread, and leave the main thread alive. By doing that, you can terminate a task, but also keep the ability to start another task without restarting the app.

Solution 11 - Java

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

Solution 12 - Java

This should do it in the correct way:

mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
mainFrame.addWindowListener(new WindowListener() {

@Override
public void windowClosing(WindowEvent e) {
    if (doQuestion("Really want to exit?")) {
      mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      mainFrame.dispose();
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOlaseniView Question on Stackoverflow
Solution 1 - JavaJonView Answer on Stackoverflow
Solution 2 - JavaChris CooperView Answer on Stackoverflow
Solution 3 - JavaBastienView Answer on Stackoverflow
Solution 4 - JavaChristian SemrauView Answer on Stackoverflow
Solution 5 - JavaJoshuaView Answer on Stackoverflow
Solution 6 - JavaFreiheitView Answer on Stackoverflow
Solution 7 - JavaVaishak SureshView Answer on Stackoverflow
Solution 8 - JavaAntonView Answer on Stackoverflow
Solution 9 - JavaDude BroView Answer on Stackoverflow
Solution 10 - JavaSmartFingersView Answer on Stackoverflow
Solution 11 - Javapooploser0110View Answer on Stackoverflow
Solution 12 - Javauser3596517View Answer on Stackoverflow