• Post Reply Bookmark Topic Watch Topic
  • New Topic

terminating a console program  RSS feed

 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • 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!
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • 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
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • 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
 
Jack Kay
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • 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;
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!