LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   how to find out if a java program called inside a shell script throws an exception (https://www.linuxquestions.org/questions/linux-general-1/how-to-find-out-if-a-java-program-called-inside-a-shell-script-throws-an-exception-949448/)

author_unknown 06-09-2012 10:23 PM

how to find out if a java program called inside a shell script throws an exception
 
Hi all,

I have a java program (not developed by me) that is being called in a shell script.

I need to modify the shell script to catch if the java program throws an exception.
Unfortunately the exit status of the step where the java program is being executed is always zero whether the java program works fine or throws an error.

Thanks,
Auth0r_Unkn0wn

pan64 06-11-2012 07:14 AM

inside java you should handle exceptions and set a return code. When java exits this return code can be passed to the caller script. If this java app did not handle exceptions you will never get a different exit code.

414N 06-11-2012 07:40 AM

I agree with pan64: the place where you should manage exceptions is inside the program, not using bash.
Given this, I think you can detect whether an exception occurred during program execution if you search for the word "Exception" in stderr output:
Code:

TEMPFILE=`mktemp`
java ..... 2>"$TEMPFILE"
if [ `grep 'Exception' "$TEMPFILE"` ]
then
    # An exception occurred. You could read which one in the first line of the $TEMPFILE file
fi
rm "$TEMPFILE"



All times are GMT -5. The time now is 04:21 PM.