Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between System.exit(int code) and Runtime.getRuntime().exit(int code)

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?

like image 846
Dima Avatar asked Jul 29 '11 15:07

Dima


People also ask

What is the difference between system Exit 0 and system Exit 1 in Java?

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.

Which of the following statement about Runtime exit () and runtime halt () is true?

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.

What is System exit ()?

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.

What can I use instead of system exit in Java?

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.


2 Answers

Look at the source. System calls Runtime:

public static void exit(int status) {
  Runtime.getRuntime().exit(status);
}
like image 76
Brian Kent Avatar answered Sep 19 '22 05:09

Brian Kent


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.

like image 28
RMT Avatar answered Sep 19 '22 05:09

RMT