Originally posted by Jesper de Jong:
Use System.exit(returncode) to end the application (see the API documentation).
[ September 06, 2005: Message edited by: Jesper de Jong ]
Using System.exit(n) is probably the best way to return an error code, in a simple Java application.
However, in a complicated system, it is less than ideal. System.exit() kills the whole Java process. It does not wait for non-daemon threads to finish, so important tasks might get terminated at a bad time.
Further, if your code might get incorporated into some larger overall application framework, then System.exit() is a bit of a disaster because it will terminate the whole application framework, not just your code.
Using the standard Java launcher (e.g. java.exe on Windows), there is no way to control the process exit code, except System.exit(). However, if you write or obtain a different launcher, alternative facilities may be added. For example, I have a launcher with a native method setExitCode(), through which I can set the code that the Java process will use, when it finally exits. Writing launchers Google for "Java Invocation Interface".