LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 06-09-2012, 10:23 PM   #1
author_unknown
Member
 
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.

Thanks,
Auth0r_Unkn0wn

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

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
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
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
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"
 
  


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:08 PM.

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