In Java, What is the difference with or without System.exit(0)
in following code?
public class TestExit
{
public static void main(String[] args)
{
System.out.println("hello world");
System.exit(0); // is it necessary? And when it must be called?
}
}
The document says: "This method never returns normally." What does it mean?
Though answer was really helpful but some how it missed some more extra detail. I hope below will help understand the shutdown process in java, in addition to the answer above:
PS: The JVM can shut down in either an orderly or abrupt manner.
In applications that may have complex shutdown hooks, this method should not be called from an unknown thread. System.exit
never exits normally because the call will block until the JVM is terminated. It's as if whatever code is running that has the power plug pulled on it before it can finish. Calling System.exit
will initiate the program's shutdown hooks and whatever thread that calls System.exit
will block until program termination. This has the implication that if the shutdown hook in turn submits a task to the thread from which System.exit
was called, the program will deadlock.
I'm handling this in my code with the following:
public static void exit(final int status) {
new Thread("App-exit") {
@Override
public void run() {
System.exit(status);
}
}.start();
}
The method never returns because it's the end of the world and none of your code is going to be executed next.
Your application, in your example, would exit anyway at the same spot in the code, but, if you use System.exit. you have the option of returning a custom code to the enviroment, like, say
System.exit(42);
Who is going to make use of your exit code? A script that called the application. Works in Windows, Unix and all other scriptable environments.
Why return a code? To say things like "I did not succeed", "The database did not answer".
To see how to get the value od an exit code and use it in a unix shell script or windows cmd script, you might check this answer on this site
In that case, it's not needed. No extra threads will have been started up, you're not changing the exit code (which defaults to 0) - basically it's pointless.
When the docs say the method never returns normally, it means the subsequent line of code is effectively unreachable, even though the compiler doesn't know that:
System.exit(0);
System.out.println("This line will never be reached");
Either an exception will be thrown, or the VM will terminate before returning. It will never "just return".
It's very rare to be worth calling System.exit()
IME. It can make sense if you're writing a command line tool, and you want to indicate an error via the exit code rather than just throwing an exception... but I can't remember the last time I used it in normal production code.
If you have another program running in the JVM and you use System.exit, that second program will be closed, too. Imagine for example that you run a java job on a cluster node and that the java program that manages the cluster node runs in the same JVM. If the job would use System.exit it would not only quit the job but also "shut down the complete node". You would not be able to send another job to that cluster node since the management program has been closed accidentally.
Therefore, do not use System.exit if you want to be able to control your program from another java program within the same JVM.
Use System.exit if you want to close the complete JVM on purpose and if you want to take advantage of the possibilities that have been described in the other answers (e.g. shut down hooks: Java shutdown hook, non-zero return value for command line calls: How to get the exit status of a Java program in Windows batch file).
Also have a look at Runtime Exceptions: System.exit(num) or throw a RuntimeException from main?
Java Language Specification says that
Program Exit
A program terminates all its activity and exits when one of two things happens:
All the threads that are not daemon threads terminate.
Some thread invokes the exit method of class Runtime or class System, and the exit operation is not forbidden by the security manager.
It means that You should use it when You have big program (well, at lest bigger than this one) and want to finish its execution.
System.exit(0)
terminates the JVM. In simple examples like this it is difficult to percieve the difference. The parameter is passed back to the OS and is normally used to indicate abnormal termination (eg some kind of fatal error), so if you called java from a batch file or shell script you'd be able to get this value and get an idea if the application was successful.
It would make a quite an impact if you called System.exit(0)
on an application deployed to an application server (think about it before you try it).
One should NEVER call System.exit(0)
for these reasons:
Quitting the program "normally" provides the same exit code to the operating system as System.exit(0)
so it is redundant.
If your program cannot quit "normally" you have lost control of your development [design]. You should have always full control of the system state.
By the way: Returning other return codes than 0 does make sense if you want to indicate abnormal program termination.
System.exit is needed
In your case, it does the exact same thing as the simple return-from-main.
Source: Stackoverflow.com