aspose file tools*
The moose likes Java in General and the fly likes Catch application closed in console Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Catch application closed in console" Watch "Catch application closed in console" New topic
Author

Catch application closed in console

Susilo Saja
Ranch Hand

Joined: May 27, 2003
Posts: 91
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

Joined: Sep 27, 2002
Posts: 51
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 ]

SJCP 1.2
Susilo Saja
Ranch Hand

Joined: May 27, 2003
Posts: 91
Thanks a lot Peter.
This method is the one I've been looking for.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Catch application closed in console