aspose file tools*
The moose likes Java in General and the fly likes Determining the exiting of a class - help!!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Determining the exiting of a class - help!!!!" Watch "Determining the exiting of a class - help!!!!" New topic
Author

Determining the exiting of a class - help!!!!

Raymond Tanner
Greenhorn

Joined: Jan 09, 2004
Posts: 24
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!
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
If someone is abruptly terminating the JVM, there's nothing your running Java program can do about it.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Scotty Sinclair
Greenhorn

Joined: Jun 26, 2003
Posts: 17
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.
Raymond Tanner
Greenhorn

Joined: Jan 09, 2004
Posts: 24
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
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
Raymond Tanner
Greenhorn

Joined: Jan 09, 2004
Posts: 24
Thanks Jim. I will give this a try.
 
 
subject: Determining the exiting of a class - help!!!!