Skip to main content

How to Exit a Java Program with an Exit Code

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Every program ends with an exit code: 0 when it succeeded, anything else when it didn't. Shells, scripts and CI jobs read that number to decide what happens next. In Java, System.exit(1) ends the program and reports a failure.

Syntax​

Syntax
System.exit(<status>);
PartWhat it does
System.exitShuts the Java Virtual Machine down; the code after it never runs.
<status>The exit code: 0 for success, a non-zero number such as 1 for failure.

The JDK 25 documentation for System.exit: "The argument serves as a status code. By convention, a nonzero status code indicates abnormal termination." It adds that the method "neither returns nor throws an exception".

The Example​

The example checks whether all orders are paid. They aren't, so it prints an error and exits with code 1.

Prerequisites​

  • A JDK, the Java Development Kit. java Main.java runs a single file without compiling it first. This guide uses OpenJDK 25.0.5.1.

Step 1: Write the Code​

Create a file named Main.java:

Main.java, which sets allPaid to false and, when it isn't true, prints Some orders are not paid. to System.err and calls System.exit(1); otherwise it prints All orders are paid.

Main.java
public class Main {
public static void main(String[] args) {
boolean allPaid = false;

if (!allPaid) {
System.err.println("Some orders are not paid.");
System.exit(1);
}

System.out.println("All orders are paid.");
}
}

Step 2: Run It and Read the Exit Code​

Run the program, then ask the shell for the exit code of the last command:

Terminal
java Main.java
echo $?

A zsh terminal where java Main.java prints Some orders are not paid. and echo $? prints 1.

Output
Some orders are not paid.
1

The last line of main never ran, and the shell reported 1, so a script or CI job knows the check failed.

Common Mistakes​

MistakeWhat happensDo this instead
Printing an error but ending normallyThe exit code is 0, so a CI job carries onCall System.exit(1) on failure
Calling System.exit deep inside library codeThe whole program stops, which callers can't preventThrow an exception there and exit only in main
Reading $? after another commandIt now holds that command's codeRead it right after running the program

Troubleshooting​

SymptomLikely causeFix
echo $? prints 0 after an error messageThe program never called System.exit with a non-zero codeExit with 1 on failure
$LASTEXITCODE is emptyNo program has run yet in this PowerShell windowRun the program first

FAQ​

What exit code does an uncaught exception give?​

1. A program that ended with an uncaught NumberFormatException printed the stack trace and exited with code 1.

What does main returning normally give?​

0, unless some code called System.exit with another number.

Conclusion​

System.exit(1) ends a Java program with a failure code that the shell reads as $? in zsh or bash and $LASTEXITCODE in PowerShell. Keep the call in main, where the program's end is easy to see.

Additional Resources​