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:
If it is true why it happens if my main()
method return void
?
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?
The VM exits when
System.exit(exitCode)
is calledIn 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.