• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

catching the event before a java program will end . . .

 
Sanjeev Charla
Ranch Hand
Posts: 90
Firefox Browser Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am not in a gui program to do something before window is closing . . .

http://www.rgagnon.com/javadetails/java-0459.html show me, how to catch the sudden cancellation of a java program.

but how to know before, when a jvm is exiting normally??
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not sure I'm reading this right, but one of the ways is to set the frame's closeOperation to DO_NOTHING_ON_CLOSE,
then add your own windowListener, and in windowClosing do what you want before calling System.exit(..)
 
Peter Taucher
Ranch Hand
Posts: 174
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A window listener would react on behalf of the window. Some applications don't necessarily run in window mode. A shutdown hook would always be possible (depends on your targets/presets):
http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29
http://cupi2.uniandes.edu.co/site/images/recursos/javadoc/j2se/1.5.0/docs/api/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shutdown hooks are indeed the way to go. They will only not be called if the JVM is terminated forcefully (e.g. using Runtime.getRuntime().halt(), or by killing the process from the operating system)
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is probably a more helpful link about shutdown hooks; note it is a bit old however. Another (also old) link.

Shutdown hooks are nice things
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic