(individual question from IDE-Style program running )
Asked
Active
Viewed 134 times
2 Answers
3
Your best option is probably to fork off a new JVM through ProcessBuilder
.
It is however possible to kill the internal program (and all the threads it spawned) using ThreadGroups. (I don't recommend it though. It uses the stop
method which according to docs is "Deprecated. This method is inherently unsafe. See Thread.stop() for details."):
public class Main {
public static void main(String args[]) throws InterruptedException {
ThreadGroup internalTG = new ThreadGroup("internal");
Thread otherProcess = new Thread(internalTG, "Internal Program") {
public void run() {
OtherProgram.main(new String[0]);
}
};
System.out.println("Starting internal program...");
otherProcess.start();
Thread.sleep(1000);
System.out.println("Killing internal program...");
internalTG.stop();
}
}
class OtherProgram {
public static void main(String[] arg) {
for (int i = 0; i < 5; i++)
new Thread() {
public void run() {
System.out.println("Starting...");
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Stopping...");
}
}.start();
}
}
Output:
Starting internal program...
Starting...
Starting...
Starting...
Starting...
Starting...
Killing internal program...

aioobe
- 413,195
- 112
- 811
- 826
-
I dislike this answer. You, as a programmer, should know that all good IDEs have a buttton that does what I want this one to do, so it must be possible. – Ky - Oct 29 '10 at 19:04
-
Oh, wow... well, that's quite the edit, there. Nevermind that last comment. Why don't you recommend this? – Ky - Oct 29 '10 at 19:05
-
I changed my answer a bit... still though, this is most likely *not* how good IDEs solve it ;) – aioobe Oct 29 '10 at 19:06
-
No. Eclipse has it's own compiler... that sort of indicates how complicated their solution could be... – aioobe Oct 29 '10 at 20:02
0
- Run the code in a separate JVM. Use debugger interfaces to control that JVM.
- Instrument the byte code of the classes you intend to run. Insert cancellation checks at appropriate places, as well as trapping access to global JVM resources.
The second choice is probably an endless source of annoying bugs.

Darron
- 21,309
- 5
- 49
- 53