• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Catch application closed in console

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Peter.
This method is the one I've been looking for.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic