AndreaNobili
AndreaNobili

Reputation: 43057

How exactly works the Java application exit code of the main() method?

I have the following doubts related a simple command line Java application.

So I have this command line application that is started by a main() method defined inside a Main class. As usual this main() method is defined with this signature:

public static void main(String[] args) {

It's return type is void, and that should mean it doesn't return any value. But when its execution correctly terminates I obtain following message in the IntelliJ console.

Disconnected from the target VM, address: '127.0.0.1:54090', transport: 'socket'

Process finished with exit code 0

What exactly does represent the exit code 0? I think it means that the program have correctly completed its execution without incur into any error.

So now I have the following 2 doubts:

  1. If it is true why it happens if my main() method return void?

  2. How can I return a different exit code if my application ended with an error?

Is there a standard exit code value for ending with errors?

Upvotes: 27

Views: 41056

Answers (4)

auntyellow
auntyellow

Reputation: 2573

Is there a standard exit code value for ending with errors?

Yes. Exit code is 1 if Exception (or other Throwable) is thrown.

$ cat Test.java
public class Test {
    public static void main(String[] args) {
        throw new Error();
    }
}
$ java Test
Exception in thread "main" java.lang.Error
    at Test.main(Test.java:3)
$ echo $?
1

Upvotes: 0

NickJ
NickJ

Reputation: 9579

An exit code of 0 means it completed normally, that is standard for all processes, not just java. The value is not returning from the main method (it's void) but from the JVM itself.

A different value can be specified, e.g. System.exit(1) to indicate some error condition, and the program stops.

Upvotes: 6

AlexR
AlexR

Reputation: 115388

Exit code of process is what process reports to operating system as its error code.

Java designers could make main() method to return int so that JVM could report to OS this value as a process exit code. But they decided to make main void but provide API that can update this code using System.exit(exitCode). The advantage of this solution is that program can decide to exit at any point and in any thread, not only in main method and in main thread.

Upvotes: 12

JB Nizet
JB Nizet

Reputation: 692181

The VM exits when

  • all of the non-daemon threads stop running, or
  • System.exit(exitCode) is called

In the first case, the exit code is 0. In the second case, it's the exit code passed to the exit() method.

Don't forget that even if your main() method returns, the program will continue running until no non-daemon thread runs anymore. And any thread running in the VM can choose to exit explicitely.

The exit code 0 means that everything went as expected. you can choose to use any other exit code to signal an exceptional condition to the environment.

Upvotes: 37

Related Questions