19

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?

1
  • 1
    The first is much shorter to write. c.f. System.gc() vs Runtime.getRuntime().gc(). Commented Jul 29, 2011 at 15:34

3 Answers 3

20

Look at the source. System calls Runtime:

public static void exit(int status) {
  Runtime.getRuntime().exit(status);
}
Sign up to request clarification or add additional context in comments.

Comments

17

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.

1 Comment

+1. That it is "the conventional and convenient means" correlates with being "more appropriate" as the OP asked.
1

The call System.exit(int status) is effectively equivalent to the call: Runtime.getRuntime().exit(int status)

System.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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.