Shutdown hook doesn't work in Eclipse

urir picture urir · Oct 11, 2012 · Viewed 18.2k times · Source

I have added a shutdown hook via:

Runtime.getRuntime().addShutdownHook(myShutdownHook);

It works fine normally, but not when I click the red stop button in Eclipse. Is there a way to make the shutdown hook be called in Eclipse?

Answer

Zoltán picture Zoltán · Oct 11, 2012

The red stop button forcibly kills the application, i.e. not gracefully, so the JVM doesn't know that the application is exiting, therefore the shutdown hooks are not invoked.

Unfortunately, there is no way (in Windows, at least) to provide a mechanism that ensures that the hook is always invoked. It's just something that may be invoked, but there is no guarantee.