How do I capture a program halt / stop event?

Posted on 2005-04-18
Last Modified: 2010-05-18
I wanted to know how to capture a CTRL-C or program stop event, and execute some shutdown code prior to the program quitting. Can anyone tell me how to do this?

Worth 500 points.

Question by:richardsimnett
    LVL 92

    Accepted Solution

    LVL 2

    Expert Comment

    Do someting like this:

    public void main(String args[])
           Runtime.getRuntime().addShutdownHook(new Thread()
                public void run()



    Author Comment

    Ok guys,
    Both of your solutions work sorta. Meaning they work from the command prompt... the java app I need this for runs as a service under windows, and does not seem to work when a "net stop" event occurs. Can you guys tell me how to catch this and make sure the shutdown process runs?


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    761 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

    10 Experts available now in Live!

    Get 1:1 Help Now