Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 06-09-2012, 10:23 PM   #1
Registered: Aug 2007
Location: Buffalo, NY
Distribution: Fedora 7, RHEL5, CentOS 5 and Backtrack 2.0
Posts: 73

Rep: Reputation: 15
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.


Last edited by author_unknown; 06-09-2012 at 11:33 PM.
Old 06-11-2012, 07:14 AM   #2
LQ Guru
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 11,350

Rep: Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418
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.
Old 06-11-2012, 07:40 AM   #3
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 635

Rep: Reputation: 186Reputation: 186
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:
java ..... 2>"$TEMPFILE"
if [ `grep 'Exception' "$TEMPFILE"` ]
    # An exception occurred. You could read which one in the first line of the $TEMPFILE file


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find out which shell script called? gurubbc Linux - Newbie 1 12-06-2010 01:00 AM
How can I call a C program in a Shell Script from Java habibur Programming 3 10-24-2009 02:09 AM
Linux based java decompiler to be called from shell script rakeshrau Linux - Newbie 2 06-25-2009 08:40 AM
Shell funcionality inside script/program wjtaylor Programming 3 05-31-2009 01:53 PM
Starting Java from shell script (Exception in thread "main") rolf_mueller Linux - Software 5 10-30-2004 02:11 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:41 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration