jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Detect windows shutdown Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Detect windows shutdown" Watch "Detect windows shutdown" New topic

Detect windows shutdown

Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 439

I want to gracefully exit my Java program that runs as a background service on windows shutdown. No Dialogs at shutdown announcing that This program must be exit... No computer hangups...

Can JACOB or JAWIN help me in this regard?

Thanx in Advance,

Maki Jav

Help gets you when you need it!
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
I had similar problem with a Java proxy server.
I solved it by creting an empty invisible AWT window, calling pack() on it and adding WindowListener to it. It will receive windowClosing event on windows shutdown.

I seems that on Windows, a window is requires even for some nonGUI tasks. Maybe that's why the OS is called Windows.
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 439
Thanks Valdo,

Following ur idea I have come up with this class

I am writing messages to a file in that method of TestRunMethod class.

This class is working for windows 98 ie when I logoff, restart or shutdown

This class will be helpful to others too.

Batch file running this has code

C:\jdk1.3\bin\javaw.exe TestAwtWindow

So when windows is exiting no command prompt is present to halt it on the way.

Thanks once again.

Maki Jav
Michael Lloyd Lee

Joined: Sep 07, 2005
Posts: 22
What is wrong with using a shutdown hook?

Should your application really care if the system is being shut down, or just your application is being shutdown?
[ October 14, 2005: Message edited by: Michael Lloyd Lee ]

Please please please use code tags!<br /> <br /><a href="http://java.sun.com/j2se/1.5.0/docs/api/" target="_blank" rel="nofollow">Java API</a> - <a href="http://java.sun.com/docs/books/tutorial/index.html" target="_blank" rel="nofollow">Java Tutorials</a>
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 439
The problem with shutdown hook is that I could get it respond to user using
CTRL + C keys on a command prompt window running my java app. It did not do anything if user closes it by clicking on X (close). What if my non gui java app is running invisibly?

My application has to do some things on system shutdown...

ShutdownHook does not respond to system shutdowns...

Maki Jav
I agree. Here's the link: http://aspose.com/file-tools
subject: Detect windows shutdown
It's not a secret anymore!