Operators ·
‹ Previous Next ›
⏱ 5 min read Modified: 2026-07-13

Java System.exit() Method

The System.exit() method in Java is used to exit a program: it terminates the currently running Java Virtual Machine (JVM) along with all of its threads. The method takes a single argument of type int — the exit code (also called exit status). By convention, 0 means the program finished successfully, while any non-zero value tells the operating system that the program ended with an error.

How System.exit() Works

Calling System.exit(status) is a shortcut for Runtime.getRuntime().exit(status). The call starts the JVM shutdown sequence:

  • all registered shutdown hooks are executed;
  • after that, the virtual machine stops and returns the given exit code to the operating system.

The key point: System.exit() never returns control to the code that called it. No statement placed after the call will run — no matter which method the call was made from or how deep in the call stack it happened.

Exit Codes: 0, 1, and Others

The exit code is a number that a process returns to the operating system. By the convention followed on most operating systems:

Exit code Meaning When it is used
0 Successful termination The program completed normally, without errors
1 General error The most common code for abnormal termination
Other non-zero values A specific error type The application defines its own meaning (for example, 2 — invalid arguments)

The exit code matters when Java programs are launched from shell scripts and build systems: it is how a script, a CI server, or a task scheduler knows whether the program succeeded. You can check the exit code of the last process like this:

# Linux / macOS
java SysExitExample
echo $?

# Windows (cmd)
java SysExitExample
echo %ERRORLEVEL%

Note: on Unix-like systems the OS keeps only the lowest byte of the code, so the meaningful range of values is 0 to 255.

System.exit() Example

Here is a simple example:

public class SysExitExample {
    public static void main(String[] args) {
        System.out.println("Before exit.");
        method(true);
        System.out.println("This line will not be executed.");
    }

    public static void method(boolean flag) {
        if (flag) {
            System.exit(0);
        }
        System.out.println("This line inside method will not be executed.");
    }
}

Output of the program:

Before exit.

The program printed only the first line: the System.exit(0) call inside method() stopped the JVM, so neither the rest of the method nor the code in main() after the call was executed.

Easy to get wrong

If System.exit() is called inside a try block, the finally block does not run: the JVM stops immediately, without unwinding the call stack. This is a popular interview question — resources you expected to close in finally will stay open.

System.exit() vs return

Beginners sometimes confuse System.exit() with the return statement, but they work quite differently:

  • return exits only the current method and hands control back to the caller. The program keeps running. See the dedicated lesson on the Java return statement for more details.
  • System.exit() ends the entire program: every thread stops, and the JVM shuts down.

A special case is return in the main() method. It finishes main(), but the JVM keeps running if any live non-daemon threads remain. System.exit() stops the JVM regardless.

When Not to Use System.exit()

In simple console programs System.exit() is perfectly appropriate — for example, to return a non-zero exit code when command-line arguments are invalid. But in some situations calling it is considered bad practice:

  • Web applications and server-side code. Calling System.exit() would bring down the whole application server together with every other deployed application.
  • Libraries. Library code should not decide for the application when to terminate — throw an exception instead.
  • Unit tests. A System.exit() call in the code under test will kill the test runner process.

In regular program logic, use return to leave a method and exceptions to report errors.

Frequently Asked Questions

What does System.exit(0) mean in Java?

System.exit(0) immediately terminates the program with exit code 0, which by convention signals successful completion without errors. The code is returned to the operating system, where scripts that launched the program can check it.

What is the difference between System.exit(0) and System.exit(1)?

For the JVM there is no difference — the program terminates either way. The difference is what the code means to the operating system: 0 indicates success, while 1 (or any other non-zero value) indicates failure. External scripts and CI systems rely on this code to judge the result of the run.

Does the finally block run after System.exit()?

No. System.exit() stops the JVM without unwinding the call stack, so finally blocks and any code after try-catch never execute. Only registered shutdown hooks are run.

How is System.exit() different from Runtime.halt()?

System.exit() starts the normal shutdown sequence: shutdown hooks run first, then the JVM stops. Runtime.getRuntime().halt() stops the JVM instantly, skipping shutdown hooks. Halt is reserved for extreme cases, for example when the shutdown sequence itself hangs.

Comments

Please log in or register to have a possibility to add comment.