In Java, System.exit(status)terminates the running Java Virtual Machine and passes status to whoever started the program: a shell, a script, a build tool, or a scheduler. By convention 0 means success and any nonzero value means an error.
The call never returns. Code after it, including finally blocks that are still pending, does not run. Registered shutdown hooks do run before the JVM stops. When main simply finishes (and no other non-daemon threads are running), the exit status is 0 without any call.
A static method of the java.lang.System class, available since the first version of Java (JDK 1.0). Call it on the class name, as in System.exit(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass the status number. Scripts can then check it:
Java
System.exit(0); // success
System.exit(1); // general error
System.exit(2); // for example: wrong command-line usage
Call
What it does
System.exit(0)
Stops the JVM and reports success.
System.exit(1)
Stops the JVM and reports an error.
Runtime.getRuntime().exit(n)
The method System.exit calls; same behaviour.
Runtime.getRuntime().addShutdownHook(t)
Registers a thread that runs during the shutdown.
Check the status after the program ends: echo %ERRORLEVEL% in Windows Command Prompt, echo $LASTEXITCODE in PowerShell, and echo $? on Linux and macOS. Shells usually see only the low 8 bits, so keep codes between 0 and 255.
Library declarations
Java
// java.lang.System
public static void exit(int status)
Parameters
Parameter
Description
status
Exit status for the operating system. 0 = success; nonzero = error. The meaning of each nonzero value is up to you.
Return Value
Return Value
Description
void
Never returns normally: the JVM shuts down.
Compare with Similar Methods
Ways a Java program can end:
Method
How it differs
return from main
Ends normally with status 0 once no non-daemon threads are left.
Uncaught exception in main
Prints a stack trace to System.err and usually ends with status 1.
Runtime.getRuntime().halt(n)
Stops immediately without running shutdown hooks; for emergencies only.
Unrelated to exiting; finalizers are not guaranteed to run at exit.
Java Examples
Example 1 — Stop with Status 1 When the Configuration Is Invalid
The program cannot start a server with an invalid port, so it prints a message and exits with an error status. The exit code is not printed; check it in the shell.
Java
public class SystemExitStatus {
public static void main(String[] args) {
String portText = "80a";
int port;
try {
port = Integer.parseInt(portText);
} catch (NumberFormatException e) {
System.out.println("Invalid port: " + portText);
System.out.println("Stopping with exit status 1");
System.exit(1);
return;
}
System.out.println("Starting server on port " + port);
}
}
Output
Invalid port: 80a
Stopping with exit status 1
How It Works
A bad setting. "80a" is not a whole number, so Integer.parseInt throws NumberFormatException and the catch block runs.
Report and stop. After the two messages, System.exit(1) ends the JVM; a script running echo $? or echo %ERRORLEVEL% afterwards sees 1.
Why the extra return. The compiler does not know that exit never returns, so without return it would complain that port might not be initialized in the last line.
Example 2 — System.exit() Skips finally Blocks
A finally block normally always runs. System.exit is the big exception: the JVM stops before it gets there.
Java
public class SystemExitFinally {
public static void main(String[] args) {
try {
System.out.println("Inside try");
System.exit(0);
} finally {
System.out.println("Inside finally");
}
System.out.println("After try");
}
}
Output
Inside try
How It Works
First line prints. "Inside try" is written before the exit call, so it appears normally.
The JVM stops inside try.System.exit(0) starts the shutdown and never returns, so control never reaches the finally block.
Nothing after it runs. Neither "Inside finally" nor "After try" is printed, and the exit status is 0. Put cleanup that must happen on exit into a shutdown hook instead.
Example 3 — Run Cleanup Code with a Shutdown Hook
Shutdown hooks are threads that the JVM starts when it shuts down, including after System.exit. They are the right place for last-minute cleanup.
Java
public class SystemExitHook {
public static void main(String[] args) {
Runtime.getRuntime().addShutdownHook(new Thread(() -> System.out.println("Shutdown hook: closing log file")));
System.out.println("Work done, exiting with status 3");
System.exit(3);
System.out.println("This line never prints");
}
}
Output
Work done, exiting with status 3
Shutdown hook: closing log file
How It Works
Register first.addShutdownHook takes a Thread that has not been started; here its body is a lambda that prints one line.
Exit triggers the hook.System.exit(3) begins the shutdown, and the JVM starts every registered hook before it stops, so the hook's message comes right after the program's message.
Status 3 reaches the shell. The line after exit never runs, and the operating system receives 3 as the exit code, which a script could use to tell this error apart from others.
Frequently Asked Questions
Both stop the JVM. The number is the exit status: 0 tells the caller that the program succeeded, and 1 (or any nonzero value) tells it that something went wrong.
No. System.exit never returns, so pending finally blocks and any code after the call are skipped. Shutdown hooks do run.
Right after the program ends, run echo %ERRORLEVEL% in Windows Command Prompt, echo $LASTEXITCODE in PowerShell, or echo $? on Linux and macOS.
In a command-line tool it is fine and useful for reporting errors to scripts. In libraries, web applications, and tests, throw an exception instead, because exit stops the entire JVM.
0, as long as no other non-daemon threads keep running. If main ends with an uncaught exception, the java launcher normally exits with status 1.