I have a class, I will call the class X, that deals with a file. This class can be shutdown due to a call in another class, that call being System.exit(0) - just like any other system. Also, this class my shutdown/exit if its running with a server and the user performs a ctrl command to shut down the server which shuts down the system which of course shuts down X. How can I tell X just before it shuts down to close the file it has open? If it was a window I would no how, but given its a class that derives from object I dont know what to do. Thank you!
A shutdown hook could work. A shutdown hook is a thread which is executed before the JVM terminates. I guess this would only be work if the CTRL-C sent a signal to the JVM asking it to terminate itself, instead of the process actually being killed.
Joined: Jan 09, 2004
Could you lead me down a path where I could find my info on a shutdown hook. I do not know how this works or where to begin to code this. thanks
Check out the API for addShutdownHook() in java.lang.Runtime. It's not guaranteed to work always - e.g. if someone executes a kill -9, or if someone just turns off the power. But it's about as reliable as you can get here, I think. [ January 12, 2004: Message edited by: Jim Yingst ]