Is it possible to kill a Java virtual machine from another virtual machine?

I have a Java application that runs another Java application. The launcher has a watchdog timer and receives periodic notifications from the second virtual machine. However, if no notifications are received, the second virtual machine must be killed, and the launcher will perform some additional cleaning actions.

The question is, is there a way to do this using only java? so far I have to use some proprietary methods to perform this operation, and it is somehow ugly.

Thanks!

+4
source share
9 answers

Something may be missing for me, but you cannot call the destroy() method on the Process object returned by Runtime.exec() ?

+6
source

You can use java.lang.Process to do what you want. After you have created a nested process and have a link to a process instance, you can get links to its standard and erroneous threads. You can periodically track them and call .destroy () if you want to close the process. All this may look something like this:

 Process nestedProcess = new ProcessBuilder("java mysubprocess").start(); InputStream nestedStdOut = nestedProcess.getInputStream(); //kinda backwards, I know InputStream nestedStdErr = nestedProcess.getErrorStream(); while (true) { /* TODO: read from the std out or std err (or get notifications some other way) Then put the real "kill-me" logic here instead of if (false) */ if (false) { nestedProcess.destroy(); //perform post-destruction cleanup here return; } Thread.currentThread().sleep(1000L); //wait for a bit } 

Hope this helps,

Sean

+3
source

You can also publish the service (via burlap, hessian, etc.) on the second JVM, which calls System.exit () and consumes it from the watchdog JVM. If you only want to close the second JVM when it stops sending these periodic notifications, it may not be able to answer the service call.

Calling shell commands with java.lang.Runtime.exec () is probably the best choice.

+2
source

The usual way to do this is to call Process.destroy() ... however this is not a complete solution, because when using the Sun JVM on * nix, they destroy the cards on SIGTERM, which does not guarantee the termination of the process (for this you need SIGKILL, since Well) . As a result, you cannot actually control processes using Java.

There are several open errors on this issue, see: link text

+2
source

java.lang.Process has a waitFor() method to wait for the process to complete and a destroy() method to kill the subprocess.

+1
source

OK, the essence twist is this:

I used the process API to close the second virtual machine, but this did not work.

The reason is that my second application is the Eclipse RCP Application, and I started it using the included eclipse.exe program.

However, this means that the Process API destroy () method will target the eclipse.exe process. Killing this process leaves the Java process unharmed. So, one of my colleagues wrote a small application that will kill the desired application.

So, one of the solutions for using the Process API (and removing redundant middle steps) is to get rid of running Eclipse when my first virtual machine duplicates all its functions.

I think I have to work.

+1
source

You should be able to do this with java.lang.Runtime.exec and shell commands.

0
source

You can use Java code to define the platform at runtime and hide the platform process termination command. This is truly an improvement on your current solution.

There is also Process.destroy () if you use the ProcessBuilder API

0
source

The process control is not precise, but you can start the rmi server in the Java virtual machine that you start and bind the remote instance using a method that performs any cleanup and calls System.exit (). The first vm can then call this remote method to turn off the second vm.

0
source

Source: https://habr.com/ru/post/1276689/


All Articles