This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Raymond Tanner
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If someone is abruptly terminating the JVM, there's nothing your running Java program can do about it.
 
Scotty Sinclair
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Raymond Tanner
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim. I will give this a try.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic