• 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

How to stop program?

 
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI!!!
IMPLEMENT KEY LISTENER.SO THAT WHEN PARTICULAR KEY IS PRESSED U CAN GET SYSTEM.EXIT(0) METHOD CALLED WHICH WILL EXIT THE PROGRAM.
 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic