• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Shut down java console app

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Hey guys, I have a java console app that basically just sets up some infrastructure and then spawns a quartz scheduler and for the most part the main thread goes to sleep, idling in a loop with Thread.sleep(x) in it. All the work will be done by the job classes fired off from the scheduler threads.

So how can I send this console app a termination signal from the keyboard? I thought control-c would work, but within Eclipse, anyway, it doesn't. I have a shutdown hook, so if the process is teminated I get control and can clean up, but I want to be able to signal this on command without sending it from a shell using kill.

How would you set up the main loop so that you could get, say, a ctrl-c and break out?

Thanks.
 
It's been awhile since I've done any console work in Java, but there should be a way to buffer the keyboard input. So just set your loop to Thread.sleep(1000) or something, and in the loop check the buffer for key presses. If the right combo was pressed just break; from the loop. I'll dig in some past code and see if I can find some syntax for you.
 
Thanks, Crusty. So far all I have been able to find is the command path that reads lines from the input stream...

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s;
while ((s = in.readLine()) != null && s.length() != 0)
/* etc */;

And this method to read single key presses...

int datum = System.in.read();

However nothing to get special keys like control/alt. The program is essentially a daemon with a console, and I'd rather not have it exit on a straight keypress.
 
Try creating an instance of BufferedInputStream and pass in java.lang.System.in into the constructor. This should allow you to grab the input data while your main thread is blocked.

edit: Haha, beat me to it.

As far as detecting control characters, I'm not sure how you would do it. An alternative would be to prompt the user after they hit a certain key to type in the word "exit" or something, or even just a confirmation as simple as Yes/No. That would prevent most accidental exits from someone typing in the wrong window or putting something on their keyboard.
 
Originally posted by: Crusty
Try creating an instance of BufferedInputStream and pass in java.lang.System.in into the constructor. This should allow you to grab the input data while your main thread is blocked.

edit: Haha, beat me to it.

As far as detecting control characters, I'm not sure how you would do it. An alternative would be to prompt the user after they hit a certain key to type in the word "exit" or something, or even just a confirmation as simple as Yes/No. That would prevent most accidental exits from someone typing in the wrong window or putting something on their keyboard.

Yeah I think I will have to do something like that. I just did some experimenting and turns out that regardless of which method you use the buffer has no data until EOL. You can press five keys, and System.in.available() will return 0, then press enter and it will return 5. I understand the implementation, but I wish there were a lower-level hook. Unfortunately I can't find anything.
 
Originally posted by: Markbnj
Originally posted by: Crusty
Try creating an instance of BufferedInputStream and pass in java.lang.System.in into the constructor. This should allow you to grab the input data while your main thread is blocked.

edit: Haha, beat me to it.

As far as detecting control characters, I'm not sure how you would do it. An alternative would be to prompt the user after they hit a certain key to type in the word "exit" or something, or even just a confirmation as simple as Yes/No. That would prevent most accidental exits from someone typing in the wrong window or putting something on their keyboard.

Yeah I think I will have to do something like that. I just did some experimenting and turns out that regardless of which method you use the buffer has no data until EOL. You can press five keys, and System.in.available() will return 0, then press enter and it will return 5. I understand the implementation, but I wish there were a lower-level hook. Unfortunately I can't find anything.

I was never a fan of console input in Java to be honest. It's nowhere near as simple as with C/C++.
 
Back
Top