Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java -jar command and return code 1

Status
Not open for further replies.

sqoti

Programmer
Dec 1, 2000
50
US
I am initiating a java program from unix shell script. I want to be able to get a return code another than 0 if the java program throws an exception. My first instinct is that the java program has to be re-written to throw the return code back, but I am not sure how that is done. Is my instinct right or is there another way in Uxix to get this code? Any insight would be appreciated greatly.

 
all return codes must be send by the application, not Unix. I.e. with a ksh script you would do something like:

***************
#!/bin/ksh
..
do something
..
if [error_1]
exit 1
if [error_2]
exit 2
if [error_3]
exit 3
..
exit 0
*************

so your java program must be rewritten in order to return codes different to 0 (zero) depending on what you want to return.

cheers.
 
You mentioned that you don't know how to rewrite the Java program.

Presumably, you want the script to return non-zero if the Java program terminated because of an exception. In that case, a good first try would be to surround the entire main function with a [tt]try[/tt]/[tt]catch[/tt] block, have the [tt]catch[/tt] handler respond to any [tt]Exception[/tt] (or maybe every [tt]Throwable[/tt], depending on what you want, exactly), and have it return the non-zero exit code.
 
All java programs main function return void... Since java is interpreted by the JVM, you can only get the JVM's return code. I think dumping a stack trace still causes the JVM to exit cleanly, so the return code is still 0, even though the program died abnormally. And all JWMs are not created equal, so you may be running into a very intresting problem.
 
Well, it's true that you can't simply return the process error code by returning something from the main function like you would in C or C++. However, you can call [tt]System.exit(5);[/tt] or whatever, and that should make the JVM exit with the given value.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top