I have an application whose main function is in infinite loop since its based on a polling mechanism.I would like to invoke and kill the main function from outside the application.For starting the application I can directly make a call like : MyClass.main("parameters")
but I couldn't find a way to kill the or terminate main function.
Asked
Active
Viewed 1,396 times
0

Harsh Gupta
- 339
- 4
- 20
-
1That's a system command . I want to do it from another automated process which first invokes it and then kills it. – Harsh Gupta Feb 14 '14 at 14:27
-
@Harsh Gupta: then you're out of luck. It is impossible for one Java application to stop or kill another without either using a system command or modifying the app to be stopped/killed. There is no API for it. – Michael Borgwardt Feb 14 '14 at 14:37
-
@njzk2 `killall` as the name implies will kill any java processes. Just a bad idea. If you want to kill a process kill the right one using the process id ... – Matteo Feb 14 '14 at 14:50
-
@njzk2 No need for sarcasm. StackOverflow is a public site with a large audience. killall ist almost always the wrong tool, not any reader has >10K reputation and can get the point ... – Matteo Feb 14 '14 at 15:01
-
@Matteo : sorry if i offended you. I though `killall java` was sufficiently obviously not something you want to actually do, but rather a short representation of `find the process and kill it`, but obviously you are right anyone should be able to read SO without any notion of irony. I remove my sarcastic comment. – njzk2 Feb 14 '14 at 15:05
-
@njzk2 Not offended at all :-). I just see people often killing everything and complaining because then nothing is running... I did not want to seem upset :-) – Matteo Feb 14 '14 at 15:10
2 Answers
1
Make a while loop that checks if a variable is true, and create a method to change the variable. When you want to terminate it, call the method and change it to false.

GreySwordz
- 368
- 1
- 9
-
"... from outside the application ..." It's unclear to me if the OP really meant from a separate process, but if so, more is needed here. – TypeIA Feb 14 '14 at 14:28
-
maybe he could try to catch some signal - http://stackoverflow.com/questions/2541475/capture-sigint-in-java – Leo Feb 14 '14 at 14:30
-
1Yes I have an application which is in infinite loop polling for files and **I cant change the mechanism there** . So if my application is MyApp.java and it has a main function .I want to automate the start and stop/kill the MyApp from another java file Automate.java – Harsh Gupta Feb 14 '14 at 14:32
-
1In that case, I don't think its possible. Imaging the havoc that would be caused if outside applications could call methods or change variables. Being purely speculative here, you could perhaps send a ping to the program.. – GreySwordz Feb 14 '14 at 14:47
-
1Shutdownhook will bring down JVM .. that will kill my other processes too isn't it? I want my app to start->stop->continue current process – Harsh Gupta Feb 14 '14 at 14:47
0
I would make MyApp a class that implements the Runnable interface. It would also have some method called hasMoreFile() or isComplete() or something like that. Then the Automate class would create a Thread and run MyApp in that thread. Automate would have a loop where it calls MyApp.isComplete() and, if false, sleeps for some specified period of time before checking again. If MyApp.isComplete() returns true, Automate would send a stop message to MyApp or stop the thread MyApp is running in.

Paul J Abernathy
- 993
- 2
- 12
- 25