Java:  Die on error / detect frozen?

Posted on 2008-11-17
Last Modified: 2013-11-23
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.
Question by:ecuguru
    LVL 13

    Accepted Solution

    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);}
    LVL 4

    Expert Comment

    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?

    Author Comment

    Java OutOfMemory exception indeed.
    LVL 4

    Assisted Solution

    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).

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Suggested Solutions

    Title # Comments Views Activity
    copyEndy  challenge 15 37
    ArrayLists in ArrayLists/HashMaps? 4 26
    groovy example issue 10 22
    search on network drive not working 4 22
    Can I legally transfer my OEM version of Windows to another PC?  (AKA - Can I put a new systemboard in my OEM PC?) Few of us are both IT and legal experts but we all have our own views of Microsoft's licensing rules and how they apply.  There are…
    When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now