Hi All, Does anyone know of a way to stop a Java application from outside of the application? I am doing some stuff with the Robot class and I want to be able to stop with something like ctrl-c. It cannot be stopped right now because it takes over control of the mouse and runs very fast. So basically, you can do nothing except wait for it to finish (which can be dangerous if you have bugs) or turn off the power (which I don't like to do). I hope someone can give me some ideas. Many thanks! Barry
Barry I don't know if this would work in your case but you could just have a separate thread running that would just listen for key board events. Then when you get the ctr-c event it can shut down all your other threads and get out of the app gracefully.
Thanks for your input! This sounds reasonable, however I am wondering how to set up the key listener because I believe this has to be registered on some component in a JFrame or the JFrame itself. (correct me if I am wrong) I am not using any windows, but a command window to start the program. There is really no way of the command window to have the focus so that I can stop the program. Any ideas? Thanks!!!
So what is the Robot class acting on??? All listeners need to be registered to AWT components (GUI things) to hear events. Since you don't HAVE any GUI things, listeners are probably not an option. However Cntl C will stop a program even without a listener. Without knowing the structure of your code it is difficult to know, but could you put in a System.in() command at strategic places. Then if you hit enter it continues, if you do cntl c it quits.
"JavaRanch, where the deer and the Certified play" - David O'Meara
Cindy, I have the Robot class acting on various windows (not Java) but stuff on my desktop. I start it from the command prompt, then minimize the command window and let it run. I am having an idea that I think may work. Let me know what you think. If I create a small JFrame (say 25 X 25) and let it show, I can put the KeyListener on it and let it listen for events between calls from the Robot class. So, I would call an event from Robot, let the JFrame show and have focus, then call another event from Robot, then show the JFrame, back and forth. The reason I need to dispose and show the JFrame is that it would get in the way of the calls to Robot. Do you think this could work?
Joined: Sep 29, 2000
Sounds clever to me. You could make the JFrame REALLY small and the listener could just listen for a mouseclick on it and stop the program. Tell us how it works out.