I'm working on the Java batch program that should exit with different codes based on various conditions. The program will be triggered and monitored by CA7 scheduler which will use exit code to trigger other jobs.
Apparently there are couple of ways to exit:
System.exit(int code)
and
Runtime.getRuntime().exit(int code)
Both of these methods will work, but which one is more appropreate to use?
exit(0) : Generally used to indicate successful termination. exit(1) or exit(-1) or any other non-zero value – Generally indicates unsuccessful termination. Note : This method does not return any value. The following example shows the usage of java.
exit() invokes the shutdown sequence of the underlying JVM whereas Runtime. halt() forcibly terminates the JVM process. So, Runtime. exit() causes the registered shutdown hooks to be executed and then also lets all the uninvoked finalizers to be executed before the JVM process shuts down whereas Runtime.
exit() method terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.
The main alternative is Runtime. getRuntime(). halt(0) , described as "Forcibly terminates the currently running Java virtual machine". This does not call shutdown hooks or exit finalizers, it just exits.
Look at the source. System
calls Runtime
:
public static void exit(int status) {
Runtime.getRuntime().exit(status);
}
There is no real difference, however it is just convention to use System.exit();
Source
The System.exit method is the conventional and convenient means of invoking this method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With