<p>Well behaved windows programs need to allow users to save their work when they are shutting the PC down.</p>

<p>How can I make my app detect the shutdown event? Any solution should allow the user to abort the shutdown if user selects, say "Cancel".</p>

<p>The normal Swing window closing hook doesn't work, nor does adding a shutdown hook.</p>

<p>On testing, the methods of WindowListener (windowClosing,windowClosed, etc) do not get called.</p>

<p>The answer I have accepted requires the use of platform specific code (JNI to register for WM_QUERYENDSESSION ). Isn't this a bug on Swing?</p>

<p>See <a href="http://forums.sun.com/thread.jspa?threadID=481807&amp;messageID=2246870" rel="nofollow noreferrer">http://forums.sun.com/thread.jspa?threadID=481807&amp;messageID=2246870</a></p>

[{'answer_id': 71907, 'author': 'Henry B', 'author_id': 6414, 'author_profile': 'https://Stackoverflow.com/users/6414', 'pm_score': 1, 'selected': False, 'text': '<p><a href="https://stackoverflow.com/questions/61692/how-do-i-get-my-java-application-to-shutdown-nicely-in-windows">how-do-i-get-my-java-application-to-shutdown-nicely-in-windows</a></p>\n\n<p>That might be of help</p>\n'}, {'answer_id': 71921, 'author': 'Cory', 'author_id': 11870, 'author_profile': 'https://Stackoverflow.com/users/11870', 'pm_score': 0, 'selected': False, 'text': '<p>The above seems to be the better answer.</p>\n\n<p>I can\'t find any good information on detecting window shutdown events. I guess the best possible method would be to detect weather your application is trying to close, using a window closing event or the like then ask the question.</p>\n\n<p><a href="http://www.javalobby.org/java/forums/t17933" rel="nofollow noreferrer">http://www.javalobby.org/java/forums/t17933</a></p>\n'}, {'answer_id': 71924, 'author': 'Jean', 'author_id': 7898, 'author_profile': 'https://Stackoverflow.com/users/7898', 'pm_score': 0, 'selected': False, 'text': '<p>Look for signal handling in java. when Windows closes it will send a signal to the application asking it to terminate most likely a sigterm</p>\n\n<p>see <a href="http://twit88.com/blog/2008/02/06/java-signal-handling/" rel="nofollow noreferrer">here</a> for more about this (I am not the owner of the website)</p>\n'}, {'answer_id': 71939, 'author': 'Aidos', 'author_id': 12040, 'author_profile': 'https://Stackoverflow.com/users/12040', 'pm_score': 3, 'selected': True, 'text': '<p>Write some JNI code to WM_QUERYENDSESSION message. You can get details for this from the MSDN documentation or by googling it.</p>\n\n<p>If you don\'t want to write too much C++ code to do this I can recommend the JNA library <a href="https://jna.dev.java.net/" rel="nofollow noreferrer">click here</a>. Which gives you some nice Java abstractions for C code.</p>\n'}]
['https://Stackoverflow.com/questions/71842', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/8118/']