<p>I have a Java application that launches another java application.  The launcher has a watchdog timer and receives periodic notifications from the second VM.  However, if no notifications are received then the second virtual machine should be killed and the launcher will perform some additional clean-up activities.</p>

<p>The question is, is there any way to do this using only java? so far I have to use some native methods to perform this operation and it is somehow ugly.</p>

<p>Thanks!</p>

[{'answer_id': 63807, 'author': 'James A. N. Stauffer', 'author_id': 6770, 'author_profile': 'https://Stackoverflow.com/users/6770', 'pm_score': 0, 'selected': False, 'text': '<p>You should be able to do that <code>java.lang.Runtime.exec</code> and shell commands.</p>\n'}, {'answer_id': 63850, 'author': 'sblundy', 'author_id': 4893, 'author_profile': 'https://Stackoverflow.com/users/4893', 'pm_score': 0, 'selected': False, 'text': '<p>You can have the java code detect the platform at runtime and fire off the platform\'s kill process command. This is really an refinement on your current solution.</p>\n\n<p>There\'s also <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Process.html#destroy()" rel="nofollow noreferrer">Process.destroy()</a>, if you\'re using the <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ProcessBuilder.html" rel="nofollow noreferrer">ProcessBuilder</a> API</p>\n'}, {'answer_id': 63909, 'author': 'David Webb', 'author_id': 3171, 'author_profile': 'https://Stackoverflow.com/users/3171', 'pm_score': 4, 'selected': True, 'text': '<p>I may be missing something but can\'t you call the <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Process.html#destroy()" rel="noreferrer"><code>destroy()</code></a> method on the <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Process.html" rel="noreferrer"><code>Process</code></a> object returned by <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html#exec(java.lang.String)" rel="noreferrer"><code>Runtime.exec()</code></a>?</p>\n'}, {'answer_id': 63924, 'author': 'Chris Hall', 'author_id': 5933, 'author_profile': 'https://Stackoverflow.com/users/5933', 'pm_score': 2, 'selected': False, 'text': '<p>You could also publish a service (via burlap, hessian, etc) on the second JVM that calls System.exit() and consume it from the watchdog JVM.  If you only want to shut the second JVM down when it stops sending those periodic notifications, it might not be in a state to respond to the service call.</p>\n\n<p>Calling shell commands with java.lang.Runtime.exec() is probably your best bet.</p>\n'}, {'answer_id': 63952, 'author': 'killdash10', 'author_id': 7621, 'author_profile': 'https://Stackoverflow.com/users/7621', 'pm_score': 0, 'selected': False, 'text': '<p>Not exactly process management, but you could start an rmi server in the java virtual machine you are launching, and bind a <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/rmi/Remote.html" rel="nofollow noreferrer">remote</a> instance with a method that does whatever cleanup required and calls System.exit().  The first vm could then call that remote method to shutdown the second vm.</p>\n'}, {'answer_id': 64062, 'author': 'Sean Reilly', 'author_id': 8313, 'author_profile': 'https://Stackoverflow.com/users/8313', 'pm_score': 2, 'selected': False, 'text': '<p>You can use <a href="http://java.sun.com/javase/6/docs/api/java/lang/Process.html" rel="nofollow noreferrer">java.lang.Process</a> to do what you want. Once you have created the nested process and have a reference to the Process instance, you can get references to its standard out and err streams. You can periodically monitor those, and call .destroy() if you want to close the process. The whole thing might look something like this:</p>\n\n<pre><code>Process nestedProcess = new ProcessBuilder("java mysubprocess").start();\nInputStream nestedStdOut = nestedProcess.getInputStream(); //kinda backwards, I know\nInputStream nestedStdErr = nestedProcess.getErrorStream();\nwhile (true) {\n    /*\n       TODO: read from the std out or std err (or get notifications some other way)\n       Then put the real "kill-me" logic here instead of if (false)\n    */\n    if (false) {\n        nestedProcess.destroy();\n        //perform post-destruction cleanup here\n        return;\n    }\n\n    Thread.currentThread().sleep(1000L); //wait for a bit\n}\n</code></pre>\n\n<p>Hope this helps,</p>\n\n<p>Sean</p>\n'}, {'answer_id': 64114, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p><code>java.lang.Process</code> has a <code>waitFor()</code> method to wait for a process to die, and a <code>destroy()</code> method to kill the subprocess.</p>\n'}, {'answer_id': 80748, 'author': 'Mario Ortegón', 'author_id': 2309, 'author_profile': 'https://Stackoverflow.com/users/2309', 'pm_score': 1, 'selected': False, 'text': "<p>OK the twist of the gist is as follows:</p>\n\n<p>I was using the Process API to close the second virtual machine, but it wouldn't work.</p>\n\n<p>The reason is that my second application is an Eclipse RCP Application, and I launched it using the eclipse.exe launcher included.</p>\n\n<p>However, that means that the Process API destroy() method will target the eclipse.exe process.  Killing this process leaves the Java Process unscathed.  So, one of my colleagues here wrote a small application that will kill the right application.</p>\n\n<p>So one of the solutions to use the Process API (and remove redundant middle steps) is to get away with the Eclipse launcher, having my first virtual machine duplicate all its functionality.</p>\n\n<p>I guess I will have to get to work.</p>\n"}, {'answer_id': 81137, 'author': 'Rob', 'author_id': 15317, 'author_profile': 'https://Stackoverflow.com/users/15317', 'pm_score': 2, 'selected': False, 'text': '<p>The usual way to do this is to call <code>Process.destroy()</code>... however it is an incomplete solution since when using the sun JVM on *nix destroy maps onto a SIGTERM which is not guaranteed to terminate the process (for that you need SIGKILL as well). The net result is that you can\'t do real process management using Java.</p>\n\n<p>There are some open bugs about this issue see:\n<a href="http://bugs.sun.com/view_bug.do?bug_id=4073195" rel="nofollow noreferrer">link text</a></p>\n'}]
['https://Stackoverflow.com/questions/63758', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2309/']