Return value from a Java code Return value from a Java code unix unix

Return value from a Java code


The return value of a Java application is not the return value of it's main method, because a Java application doesn't necessarily end when it's main method has finished execution.

Instead the JVM ends when no more non-daemon threads are running or when System.exit() is called.

And System.exit() is also the only way to specify the return value: the argument passed to System.exit() will be used as the return value of the JVM process on most OS.

So ending your main() method with this:

System.exit(0);

will ensure two things:

  • that your Java application really exits when the end of main is reached and
  • that the return value of the JVM process is 0


Java programs do not return an exit code back to the operating system by returning a value from main, as is done in C and C++. You can exit the program and specify the exit code by calling System.exit(code);, for example:

// Returns exit code 2 to the operating systemSystem.exit(2);


System.exit(0);

This returns error code 0 (everything went fine).System.exit Doc