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.jar
TwoJar - class from 2.jar

Tell me how to solve the problem. Thank.

  • maybe it is worth starting to start ThreadHook initialization, and then crashing 2.jar? And yes, isn't the process terminated without ThreadHook? - GenCloud
  • Can you show the code of both sides? And under what operating system does it occur? - Roman
  • Try to make related logic at an Event event, for example: getListnerList (). OnDestroy (boolean shutdown_hook). And when an event triggers, we launch your hook - GenCloud
  • @GenCloud, the problem is that ThreadHook is just in 2.jar, and for this jar just Proccess is responsible, which I stop, that is, 1.jar does not know about the class ThreadHook - ezhov_da
  • @Roman, the code is big enough, added a pseudo to the question. As a result, when a stop is called from the OneJar class, the stop from the TwoJar class is not called. Windows system. - ezhov_da

1 answer 1

Under windows it will not work in principle. In windows, the process is not notified of the impending completion, it is simply nailed (an analogue of SIGKILL ), so that it cannot execute the shutdown hook. You can see: the implementation of Process.destroyForcibly() for windows actually coincides with Process.destroy() . And in the Process.destroy() documentation you are warned: "whether the process is forcibly terminated depends on the implementation".

In windows, there is no concept of signals, with the exception of a very limited implementation of POSIX, so Java, in general, has nothing to intercept. The only thing is the ability to handle Ctrl + C in the console, but this is not your case.

To solve your problem, you should probably establish some kind of interaction between processes 1 and 2, for example, EOF on the standard input of process 2 or control via a TCP socket.

And the code you gave is somewhat strange: in the process of 1 stop() is called after process 2 has completed ( process.waitFor() ).

  • Thanks for the answer, I will dig further. In the above code, there is an inaccuracy, in a hurry, it is assumed that Proccess is started in another thread and stop () is called when it is needed by other classes. - ezhov_da
  • Solved the problem through sockets, 1.jar creates a server, 2.jar connects to it, when you need to update, close the socket, the application closes. - ezhov_da