How do I capture Java exceptions at a Linux shell command line?

I have a cron job on Linux running a Java process.

This process is creating threads.

I want to capture any Java exceptions at the shell so I can send out an email alert.

How do I capture these Java exceptions at a Linux shell command line?
pmsguyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jcott28Commented:
The cronjob, when started, would need to pipe out stdout to a file.  Just redirect it into a file:
java -jar foo.jar > output.txt

Or, you could implement some sort of logging inside of the java app.  Look up log4j and see about implementing that.
pmsguyAuthor Commented:
I want to capture the signal generated by the Java exception.

My shell script calls the program foo

# Call the java program foo
java foo

If foo has a Java exception I want to capture this error during the execution of the program and have foo abort

Note: foo creates threads also, how do I capture exceptions in threads and have them abort to the shell?


HegemonCommented:
It depends on how the application itself manages the exception. It can handle, swallow, log or crash and exit.. In the first two cases you cannot know about the exceptions from the shell script. If the application exits, the script will notice.

In the most likely case the application logging the exception to standard output or a file, the script can periodically check the presence of a certain string in the file.
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

pmsguyAuthor Commented:
Most of the java code has a

try {
} catch(SQLException e) {
  e.printStackTrace();
  System.exit(1);
  }

So when it does the System.exit(1) I can capture this status in the shell script.

What about threads?  If they have a
try {
} catch(SQLException e) {
  e.printStackTrace();
  System.exit(1);
  }

will all the threads and the main process die?
HegemonCommented:
- So when it does the System.exit(1) I can capture this status in the shell script.

Yes and you can analyse  the exit code.

- will all the threads and the main process die?

If you use System.exit(1)  - yes, it kills the Java process and all its threads.

See also here: http://www.javapractices.com/topic/TopicAction.do?Id=86
pmsguyAuthor Commented:
What about the cases where I do NOT have a try/catch in place and there is a Java exception in a thread.
Does this thread process abort?

Will the main process and all threads be killed?

If there is a java exception in the main process will all the threads be killed?
HegemonCommented:
No, only System.exit() guarantees the killing of all threads.
If all threads are daemon threads and the main process exits, the daemon threads will exit as well. Non-daemon ones will stay.

Whether they will exit after an exception really depends on the logic coded in the run() method. If there is no exception catching at all, then, even in being a loop, the thread will choke on exception and will exit. The main process will remain active.

To summarise: System.exit() terminates the OS "java" process, terminating all its threads.
When only daemon treads remain, they exit as well.
Each thread will exit if encounters an unhandled exception.
If an exception is handled, the flow depends on the handling logic.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
>>Note: foo creates threads also, how do I capture exceptions in threads and have them abort to the shell?

Threads are actually not relevant. You can only capture whatever is sent to stdout/stderr

As far as aborting is concerned, the app might either do so (nothing to do with the shell btw) or not, depending on what sort of exception is thrown. In order to abort in every case, you need to do that explicitly by calling System.exit(1)
CEHJCommented:
As far as the mailing/alerting is concerned, cron often does this by default - see man cron and ${MAILTO}

The first comment mentions methods that are to be preferred though, although you might want to do instead, something more like
java -classpath /x/y/z a.b.X 2>/var/log/myapp/err.log

Open in new window

ghostdog74Commented:
To capture any stderr messages to log file, use the file descriptor "2".

eg



command ...   2>logfile

Open in new window

CEHJCommented:
>>use the file descriptor "2".

(Already mentioned at http:#33931787 )
ghostdog74Commented:
>>(Already mentioned at http:#33931787 )


nah, there is nowhere you mentioned "use the file descriptor 2".
CEHJCommented:
java -classpath /x/y/z a.b.X **********2>**************/var/log/myapp/err.log
ghostdog74Commented:
that's a piece of code. You didn't mention anything about "use the file descriptor 2".

Come on, if there are 5 people with different explanation and solutions to the same problem that you have already answered, are you going to tell them one by one that its already been answered (or mentioned) ?

pmsguyAuthor Commented:
good
CEHJCommented:
pmsguy, can you tell me why the accepted answer is an answer to the question you asked?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.