How do I capture a program halt / stop event?

Hello,
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.

Thanks,
Rick
richardsimnettAsked:
Who is Participating?
 
objectsCommented:
0
 
limaidealCommented:
Do someting like this:

public void main(String args[])
{
...
       Runtime.getRuntime().addShutdownHook(new Thread()
       {
            public void run()
            {
                youShutdownCode();
                yourCleanupCode();
                ....
            }
        });

...

}
0
 
richardsimnettAuthor Commented:
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?

Thanks,
Rick
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.