How to Quit a Java App from Within the Program

Terminating a Java Program

Calling System.exit(0) (or any other value for that matter) causes the Java virtual machine to exit, terminating the current process. The parameter you pass will be the return value that the java process will return to the operating system. You can make this call from anywhere in your program - and the result will always be the same - JVM terminates. As this is simply calling a static method in System class, the compiler does not know what it will do - and hence does not complain about unreachable code.

return statement simply aborts execution of the current method. It literally means return the control to the calling method. If the method is declared as void (as in your example), then you do not need to specify a value, as you'd need to return void. If the method is declared to return a particular type, then you must specify the value to return - and this value must be of the specified type.

return would cause the program to exit only if it's inside the main method of the main class being execute. If you try to put code after it, the compiler will complain about unreachable code, for example:

public static void main(String... str) {
System.out.println(1);
return;
System.out.println(2);
System.exit(0);
}

will not compile with most compiler - producing unreachable code error pointing to the second System.out.println call.

How to quit a java app from within the program

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)

How to exit gracefully a java application?

Register a shutdown hook with the Java Runtime. The shutdown of JVM will be notified on the registered thread. Below is an example:

public class Main {

public static void main(String[] args) {

Runtime.getRuntime().addShutdownHook(new ShutDownHookThread());

while (true) {

}

}

}

class ShutDownHookThread extends Thread {
@Override
public void run() {
// ***write your code here to handle any shutdown request
System.out.println("Shut Down Hook Called");
super.run();
}
}

How to stop a java application?

Stopping (exiting) the application should be inside the application. Whether it is command line or GUI based, the application developer should write code to exit it (For eg., in a command line application you might have something like Press 5 to exit, Press Esc to Exit etc) and in an application with a GUI, you will have to write code to exit when the window is closed, or an EXIT button (or others, depending on your application)

Ctrl + C is KILL the application. This is not a Normal exit. For apps with a GUI, the user would typically (in Windows) go to task manager and end the process (similar ways in other operating systems)

But these are abnormal exits - when the user wants to kill the app when, for instance, the application is no longer responding. Normal exits should be provided by the application (and therefore by the programmer - you)

Program stays in memory after close

If you want to shut down the JVM, you can use System.exit(0);. Your code only disposes the interface, it doesn't stop the program.



Related Topics



Leave a reply



Submit