Reputation: 40721
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?
Upvotes: 198
Views: 371213
Reputation: 755
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.
Upvotes: 7
Reputation: 36577
System.exit()
can be used to run shutdown hooks before the program quits. This is a convenient way to handle shutdown in bigger programs, where all parts of the program can't (and shouldn't) be aware of each other. Then, if someone wants to quit, he can simply call System.exit()
, and the shutdown hooks (if properly set up) take care of doing all necessary shutdown ceremonies such as closing files, releasing resources etc.
"This method never returns normally." means just that the method won't return; once a thread goes there, it won't come back.
Another, maybe more common, way to quit a program is to simply to reach the end of the main
method. But if there are any non-daemon threads running, they will not be shut down and thus the JVM will not exit. Thus, if you have any such non-daemon threads, you need some other means (than the shutdown hooks) to shut down all non-daemon threads and release other resources. If there are no other non-daemon threads, returning from main
will shut down the JVM and will call the shutdown hooks.
For some reason shutdown hooks seem to be an undervalued and misunderstood mechanism, and people are reinventing the wheel with all kind of proprietary custom hacks to quit their programs. I would encourage using shutdown hooks; it's all there in the standard Runtime that you'll be using anyway.
Upvotes: 219
Reputation: 12260
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?
Upvotes: 3
Reputation: 1916
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
Upvotes: 15
Reputation: 381
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.
Upvotes: 7
Reputation: 60758
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();
}
Upvotes: 12
Reputation: 11433
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.
Upvotes: 4
Reputation: 18435
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).
Upvotes: 13
Reputation: 7388
System.exit is needed
In your case, it does the exact same thing as the simple return-from-main.
Upvotes: 5
Reputation: 1500065
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.
Upvotes: 49