Skip to content
Advertisement

Return Java system exit value to bash script

I am trying to get the return value from a java program ( System.exit(1);) into a shell script, but it seems like its returning the jvm exit code, which is always 0, if it doesnt crash. For testing purposes, this is the very first line in my main(). Anyone know how to do this?

My bash code:

JavaScript

Thanks

Advertisement

Answer

If your script has only the two lines then you are not checking for the correct exit code.

I am guessing you are doing something like:

JavaScript

where script contains only:

JavaScript

Here, the script is executed in a subshell. So when you execute the script, $? is the value of last command in that shell which is nothing in the subshell. Hence, it always returns 0.

What you probably wanted to do is to call the java binary in your script itself.

JavaScript

Or simply check the exit code directly without using the script:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement