How do I get the bash command exit code from a Process run from within Java? How do I get the bash command exit code from a Process run from within Java? unix unix

How do I get the bash command exit code from a Process run from within Java?


Process.waitFor() returns an int That is where the exit code is returned. If you start an unrelated program, won't have a previous exit code which is why it doesn't return anything useful.

You can't use exitValue() reliably because you could call it before the program has finished. You should only call it after calling waitFor() (and it will give the same exit code waitFor() does)

From the Javadoc for exitValue

Throws: IllegalThreadStateException - if the subprocess represented by this Process object has not yet terminated.