How do I close a java application from the code
Asked Answered
V

5

5

How do you close a java application from the code?

Vibraphone answered 2/8, 2011 at 23:38 Comment(0)
R
15

You call System.exit:

System.exit(0);
Rurik answered 2/8, 2011 at 23:39 Comment(1)
You can give your app a chance to clean up by adding a shutdown hook: Runtime.getRuntime().addShutdownHook(myShutdownHook)Plasmo
P
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

Polanco answered 2/8, 2011 at 23:55 Comment(4)
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 ;)Etruscan
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 raptorsPolanco
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).Etruscan
The "return from end" approach won't stop the application if there are other live (non-daemon) threads in existence.Mclaurin
R
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.");
}
Rosenda answered 2/8, 2011 at 23:45 Comment(1)
You can just as easily use addShutdownHook()Etruscan
M
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.

Metal answered 2/8, 2011 at 23:44 Comment(0)
O
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)

Objectify answered 2/8, 2011 at 23:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.