aspose file tools*
The moose likes Java in General and the fly likes How to stop program? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to stop program?" Watch "How to stop program?" New topic
Author

How to stop program?

Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

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
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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.

Dave


Dave
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

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!!!

Barry
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

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?

Thanks,
Barry
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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.
sharad goswami
Ranch Hand

Joined: Jan 18, 2001
Posts: 51
HI!!!
IMPLEMENT KEY LISTENER.SO THAT WHEN PARTICULAR KEY IS PRESSED U CAN GET SYSTEM.EXIT(0) METHOD CALLED WHICH WILL EXIT THE PROGRAM.


Stop The World I Want To Get Off.
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Sharad,
Evidently you did not read all of the other posts that had to do with this topic. It is not as simple as that!

Barry
 
Don't get me started about those stupid light bulbs.
 
subject: How to stop program?