• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 891
  • Last Modified:

Java: Die on error / detect frozen?

I'm still debugging a java application I wrote, that does a lot of work with databases.  Occasionally, I get a java heap error or local memory error.  Right now, it prints out the error message and the app just stops.  It doesn't die, it just sits there with the printed out error message.

I'm running the application from a batch file:
java -jar myapp.jar

Is there a way to setup the java environment, or the app, so that on error like heap/memory, kill the app?
Best thing i to find the bug and squash it, but for resiliency, I'd like to be able to keep the app running after crash.  If it exists, I can detect that and restart it (I think) by setting it up as a service in Windows.
  • 2
2 Solutions
It is probably a JRE bug, so you should post it to the sun website.

Are you using JNI?

If instead is a bug in your code, surrond the code with this block:

//your code
catch(Exception e){e.printStackTrace();System.exit(1);}
Could you please elaborate on what type of error it is you're getting. Is it a windows error (windows msgbox popup)? Is it a java OutOfMemory exception that's thrown?
ecuguruAuthor Commented:
Java OutOfMemory exception indeed.
You can try what MicheleMarcon suggested, except you should catch Throwable, not Exception. OutOfMemory is a subclass of Error, not Exception so you'll need to catch the "super" interface (Throwable).

Just wrap the code in your main() function in a try/catch, catch Throwable and do a System.exit(1) like MichelleMarcon suggest (which should forcibly terminate your application).

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now