There is a file 1.jar that runs the file 2.jar as Runtime.getRuntime().exec("path/2.jar"); .
File 2.jar in turn, runs the process list also via Runtime.getRuntime().exec(); and holds them.
When 1.jar calls process.destroy() for 2.jar , a class has been added to this jar via Runtime.getRuntime().addShutdownHook(new ThreadHook()) , which Runtime.getRuntime().addShutdownHook(new ThreadHook()) through the list of running processes and closes them.
The problem is that when 2.jar closes through process.destroy() , this ThreadHook not called.
class OneJar { Process process; public static void main(String[] args) { process = Runtime.getRuntime().exec("run 2.jar"); process.waitFor(); stop(); } private static void stop() { Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { process.destroy(); } }); } } class TwoJar { private static List<Process> processes = new ArrayList<>(); public static void main(String[] args) { Process process; process = Runtime.getRuntime().exec("run any1.jar"); processes.add(process); process = Runtime.getRuntime().exec("run any2.jar"); processes.add(process); process = Runtime.getRuntime().exec("run any3.jar"); processes.add(process); stop(); } private static void stop() { Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { for (Process p : processes) { p.destroy(); } } }); } } OneJar - class from 1.jarTwoJar - class from 2.jar
Tell me how to solve the problem. Thank.