aspose file tools*
The moose likes Java in General and the fly likes Hooking into the windows restart or shutdown commands Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Hooking into the windows restart or shutdown commands" Watch "Hooking into the windows restart or shutdown commands" New topic
Author

Hooking into the windows restart or shutdown commands

Shawn Diotte
Greenhorn

Joined: Oct 24, 2002
Posts: 14
I was wonder if anybody knows how to take shut down a java program when the restart or the shutdown command is given in windows. What i mean is i want to be able to hook into this command and have my application shutdown automatically before windows shuts down.
Jon Strayer
Ranch Hand

Joined: Dec 04, 2002
Posts: 133
Are you sure it isn't already? From the JavaDocs for java.lang.RunTime.addShutdownHook (jdk 1.4)
The Java virtual machine shuts down in response to two kinds of events:
* The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or
* The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or system shutdown.


Jon
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Try the Shutdown Hooks API.
Of course if the system CRASHES down it might not work.
From an article:

But what if someone presses CTRL+C and shutdown the VM, or sends a kill signal in Unix/Linux?
Will your memory-buffered logfile not be written to disk, with the loss of important information as a consequence?
The only way to make some code execute right before the VM shuts down is using shutdown hooks. And that's exactly what this article will teach you.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Shawn Diotte
Greenhorn

Joined: Oct 24, 2002
Posts: 14
I know about this type of hooking but what i am looking for is when Windows goes to shutdown or restart.....is there a way for us to see that Windows is doing this and can we do stuff based on these events?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hooking into the windows restart or shutdown commands