Java Reference ยท java.lang
Java System.exit() method
Reference for System.exit(): exit status codes, shutdown hooks, what it skips (finally blocks), Runtime.halt and clean alternatives to terminating the JVM.
System.exit(int status) starts an orderly shutdown of the whole Java Virtual Machine. By convention, status 0 means success and any other value signals an error; shell scripts and CI systems read it as the program's exit code.
The call never returns normally. It runs the registered shutdown hooks, then stops the JVM, regardless of how many threads are still running. In most programs you do not need it at all: when main returns and no non-daemon threads remain, the JVM exits with status 0 by itself.
if (args.length == 0) { System.err.println("usage: tool <file>"); System.exit(2); }
Try it
Calling exit in a TryIt would end the run, so the first example uses a shutdown hook to show what happens after exit, and the second shows the effect on finally.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public static void exit(int status)
| Name | Description |
|---|---|
status |
The exit code reported to the operating system. 0 is success. Only the low 8 bits are reliably visible on Unix (0 to 255). |
Returns: never returns normally.
Throws: SecurityException if a security manager forbids it (the security manager no longer exists in current Java).
System.exit(n) is equivalent to Runtime.getRuntime().exit(n). A harder variant, Runtime.getRuntime().halt(n), stops immediately without running hooks.
Usage notes
finallyblocks and try-with-resources do not run for the code that is still on the stack whenexitis called, so files may stay unflushed. Close resources before calling it.- Shutdown hooks do run, in unspecified order and concurrently. They should be short and must not call
System.exit()themselves (that blocks). They also run on Ctrl+C orSIGTERM. - Never call it from library code. A library that terminates the JVM takes the host application down with it, and it makes the code impossible to unit test.
- In servers and frameworks,
exitkills every request in flight. Prefer to signal shutdown and let threads finish. - Exceptions are cleaner. Let an uncaught exception end
main(status 1) or catch it nearmainand map it to an exit code once. - Only one place. Keep
System.exitinmainonly; every other layer should return values or throw. - Exit codes:
0success,1general error,2usage error are common conventions. Codes above 128 are often reserved for signals (128 + signal numberon Unix shells). - Daemon threads are abandoned at exit; non-daemon threads do not stop the JVM from exiting once
exitis called. - Testing: the old security-manager trick to intercept
exitis gone; structure code somainjust calls a method that returns the status.