Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Catch application closed in console

 
Susilo Saja
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
In a java console application, how can I catch it when the user close the application (either by typing ctrl-c or clicking "X" on the console window)?
Susilo
 
peter greaves
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the method addShutdownHook() in java.lang.Runtime might help you. if you run and compile this you can cvrtl-c it during the loop and the hook class's start() will be called by the runtime.

it seems to offer you the chance to define some behaviours under the circumstances you describe. it handles ctrl-c anyway - you would have to code some testable behaviour into the run() method (e.g. write a file) for window-close because, of course, the window is closed.
note : you will need to be on a 1.3 JRE for this.
good luck
peter
[ June 13, 2003: Message edited by: peter greaves ]
 
Susilo Saja
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Peter.
This method is the one I've been looking for.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic