Search code examples
javashutdown-hook

Java ShutDownHook not being fired on Windows


Ive got the following ShutdownHook to detect when the application is exited:

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        public void run() {

            // Do what you want when the application is stopping
            sendMsg("", "goOfflineExit", "12");
        }
    }));

This works perfectly on Mac os but for some reason nothing gets fired on Windows.

Any ideas what im missing?


Solution

  • I can think of two possible explanations:

    • The sendMsg(...) call may be happening too late; e.g. after streams have been closed or flushed.

    • The sendMsg(...) call may be throwing an exception. Uncaught exceptions thrown in a shutdown hook typically don't get reported.

    If you showed us the code of sendMsg we might be able to figure out a more definite answer.