Linux, Windows and exit codes – the debug storyOctober 22nd, 2008 | by Toomas Römer | |
While running a test framework I was very pleased to see all the tests pass. The problem was that I was the only one that lucky.
The logic of the framework is quite simple, ANT build.xml starts up, runs a test as a separate java task, gathers the result code and tests for equality of -1. The test was failing for no reason.
Checking the source code of the java task it was obvious that it was returning -1. Quick grep and a debugger session showed that ANT was not mangling the return code. So System.exit(-1) was returning 255. System.exit(42) then again returned 42.
Quick google shows that this is how it is supposed to work. Quoting from steve-parker.org
Exit codes are a number between 0 and 256, which is returned by any Unix command when it returns control to its parent process. Other numbers can be used, but these are treated modulo 256, so exit -10 is equivalent to exit 246, and exit 257 is equivalent to exit 1.
And then we have Windows where -1 is a valid exit code (well, at least it works). Something to remember when have to exit on multiple platforms.