File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Catch application closed in console" Watch "Catch application closed in console" New topic

Catch application closed in console

Susilo Saja
Ranch Hand

Joined: May 27, 2003
Posts: 91
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)?
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
[ 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.
I agree. Here's the link:
subject: Catch application closed in console
It's not a secret anymore!