File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes regarding System.exit(int n) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "regarding System.exit(int n)" Watch "regarding System.exit(int n)" New topic

regarding System.exit(int n)

Keshava murthy S

Joined: Jul 20, 2006
Posts: 5
by invoking this method with an integer argument ranging between 1 to 14 we can shut down the runnibng JVM. What i know is i have to use only arguments between 1 to 14 , and they carry some meaning, and my question is what actually these constants state or represent.

thanks in advance
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15094

Where did you find that information, about that the argument should be in the range 1 to 14?

It doesn't matter what the argument is. System.exit(int n) terminates the JVM, and the value of the argument doesn't matter.

The argument is just the exit code that the JVM passes back to the operating system when the process ends. On for example Unix, you can write a shell script in which you can retrieve the exit code and use it in any way you like.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
As Jesper says, the argument to System.exit() can be any integer.

If you are running your application inside some outer program (Java or non-Java), then that outer program might impose restrictions on the exit code. Otherwise, there are no restrictions.

A very common exit code is zero. This is often interpreted by the program that ran your Java program as a successful exit, whereas non-zero is often considered to be an error code. However, this is not universally true, and you need to find out what is the case for your environment. It's nothing to do with Java, though; the same would apply to any program.

A last thought: Java programs should not use System.exit() at all, unless they really have to. A Java program will exit just fine when all the non-daemon threads have exited. It is much better to exit this way, as you can be sure that all threads have had a chance to do any necessary clean-up. If you System.exit(), some thread might be in the middle of some operation. Also, code that calls System.exit() is difficult to incorporate into a larger application (e.g. a Web application server), as System.exit() kills the whole process, not just its part of it.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257


We can pass any integer number to System.exit() method. Mostly the negative numbers represent the failure of the operation , zero success and positive number some information messages to the OS.
I agree. Here's the link:
subject: regarding System.exit(int n)
It's not a secret anymore!