• 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

terminating a console program

 
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not using Java's Console class right now--I just have a command line program that I want to run indefinitely because it's checking and logging some stuff on a server. Right now, it runs indefinitely because it's in a while(true) loop, so I was wondering if I could replace true with some code that's checking for like a Ctrl-E (for end) given by the user, or something like that. Is there any easy way to check for that? I suppose it's not really that necessary since I can just terminate the JVM, but, if there's an easy way, it'd be nice...

Thanks!
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephen,

Forgive me if this is naive but, if your app is running in the foreground, wouldn't a Ctrl-C kill it politely anyway?

Jules
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Console class?? I see a ConsoleHandler in java.util.logging, but no Console class in SDK 1.4
I just use a ctrl-C to kill such an application. If you want to get elegant, look at the "addShutdownHook" method in java.lang.Runtime.

Bill
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could have a boolean value to check if the program is done (it would be done when they hit CTRL+F).

while(!MrGlobal.done){
// do stuff
}

And then when they hit CTRL+F, go like this:

MrGlobal.done = true;
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic