5

How do you close a java application from the code?

0

5 Answers 5

15

You call System.exit:

System.exit(0);
1
  • 6
    You can give your app a chance to clean up by adding a shutdown hook: Runtime.getRuntime().addShutdownHook(myShutdownHook) Aug 2, 2011 at 23:48
5

I believe that by most standards, System.exit() is a not very OOP way of closing applications, I've always been told that the proper way is to return from main. This is somewhat a bit of a pain and requires a good design but I do believe its the "proper" way to exit

4
  • 1
    And gives you what exact benefits? In the end both variants execute pretty much the same code - just because something is convenient doesn't mean one shouldn't use it ;)
    – Voo
    Aug 3, 2011 at 0:14
  • 2
    I believe the idea is that you're supposed to unravel the call stack manually. There's no shortage of hideous ways to close your program, if you really wanted, you could make up an exception and throw it making your program crash, that would effectively exit but is not so pretty. I feel like doing anything other than the manual unraveling is akin to using a goto, which may or may not get you eaten by raptors
    – pasha
    Aug 3, 2011 at 0:40
  • 2
    Sure I can see problems with using exit() as soon as we involve threads. But then returning from main solves these elegantly by not stopping the program or has the exact same problems if those are daemon threads. shutdownhooks + system.exit seem to me the simplest working solution to really exit a program reliably (the swing solution calls system.exit in the background afaik).
    – Voo
    Aug 3, 2011 at 0:53
  • 2
    The "return from end" approach won't stop the application if there are other live (non-daemon) threads in existence.
    – Stephen C
    Aug 3, 2011 at 1:46
3

If you're terminating a Swing app, I would do an EXIT_ON_CLOSE

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

before System.exit(0). This is better since you can write a Window Listener to make some cleaning operations before actually leaving the app.

That window listener allows you to exit the app from the code:

public void windowClosing(WindowEvent e) {
    displayMessage("WindowListener method called: windowClosing.");
    //A pause so user can see the message before
    //the window actually closes.
    ActionListener task = new ActionListener() {
        boolean alreadyDisposed = false;
        public void actionPerformed(ActionEvent e) {
            if (frame.isDisplayable()) {
                alreadyDisposed = true;
                frame.dispose();
            }
        }
    };
    Timer timer = new Timer(500, task); //fire every half second
    timer.setInitialDelay(2000);        //first delay 2 seconds
    timer.setRepeats(false);
    timer.start();
}

public void windowClosed(WindowEvent e) {
    //This will only be seen on standard output.
    displayMessage("WindowListener method called: windowClosed.");
}
1
  • You can just as easily use addShutdownHook()
    – Voo
    Aug 3, 2011 at 0:13
2

If you're running an application, System.exit will work.

System.exit(int);

In an applet, however, you'll have to do something along the lines of applet.getAppletContext().showDocument("landingpage.html"); because of browser permissions. It won't just let you close the browser window.

1

You use System.exit(int), where a value of 0 means the application closed successfully and any other value typically means something was wrong. Usually you just see a return value of 1 along with a message printed to sysout or syserr if the application did not close successfully.

Everything is fine, application shut down correctly:
System.exit(0)

Something went wrong, application did not shut down correctly:
System.err.println("some meaningful message"); System.exit(1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.