Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I close a java application from the code

How do you close a java application from the code?

like image 308
Ben Avatar asked Aug 02 '11 23:08

Ben


People also ask

How do you exit a Java application?

To end a Java program, we can use the exit() method of the System class. It is the most popular way to end a program in Java. System. exit() terminates the Java Virtual Machine(JVM) that exits the current program that we are running.

How do you stop a Java code?

To stop executing java code just use this command: System. exit(1); After this command java stops immediately!

How do I close a different program in Java?

exec("taskkill /F /IM cwserv5.exe"); This will force the process with image name cwserv5.exe to end. If you don't want to force it to end, don't use the /f tag.


5 Answers

You call System.exit:

System.exit(0);
like image 113
João Silva Avatar answered Nov 07 '22 16:11

João Silva


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

like image 39
pasha Avatar answered Nov 07 '22 14:11

pasha


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.");
}
like image 41
fireshadow52 Avatar answered Nov 07 '22 14:11

fireshadow52


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.

like image 20
Peter Kazazes Avatar answered Nov 07 '22 15:11

Peter Kazazes


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)

like image 21
Nate W. Avatar answered Nov 07 '22 16:11

Nate W.