Your best bet is probably to deploy a new JVM through ProcessBuilder .
However, you can kill the internal program (and all the threads it spawned) using ThreadGroups. (I do not recommend using it). It uses the stop method, which according to the 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...
source share