28

Any time a Perl, Python, C++ or Tcl program halts with an unhandled exception, those language runtimes take care to register a non-zero exit code for the process. Even Eclipse-based programs return 1 if they fail during startup. Programs run by the standard java.exe, however, happily return zero no matter how abruptly they end, unless the program calls System.exit() with an exit value. Even AssertionFailedError or UnsatisfiedLinkError are reported back to the calling program as successful exits.

Of course not all systems have program return codes, but Unix and Windows were important enough to warrant java.lang.Process.exitValue() for child processes; don't they also warrant honouring conventions for parent processes?

Is this a flaw in the language design or just in the implementation? Is there an argument that it's a good idea?

3
  • Wonder if this is not better suited on StackOverflow... Just an opinion, though.
    – gablin
    Commented Nov 22, 2010 at 12:28
  • @gablin Mmm, indeed I started writing this question on SO then changed my mind and posted it here.
    – James
    Commented Nov 22, 2010 at 19:16
  • 6
    The java command in Java 6 and Java 7 both seem to respond with a non-zero exit code if an uncaught exception is raised.
    – dimo414
    Commented Sep 8, 2015 at 19:39

1 Answer 1

35

If the Java Language Specification does not explicitly define what exit value to expect, then it is undefined and you should not rely on it.

You need to catch Throwable in your main method, and call System.exit(1) yourself.

3
  • 8
    So isn't it a shortcoming to leave it undefined?
    – James
    Commented Nov 22, 2010 at 19:11
  • 4
    Maybe, maybe not. The only important thing is what the JLS says.
    – user1249
    Commented Nov 22, 2010 at 19:41
  • The JLS isn't the only relevant contract, though it is the dominant one. Commented Feb 13, 2020 at 12:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.