Is a Java Process
which was created via Runtime.exec
killed when the runtime that created the process dies? Or do I have to kill them manually, e.g. by installing a shut down hook and killing all remaining processes via Process.destroy
. The javadoc only speaks about what happens when the runtime continues to exist.
Asked
Active
Viewed 904 times
3

Rafael Winterhalter
- 42,759
- 13
- 108
- 192
1 Answers
3
A Java Process
is created as a subprocess of the JVM. All operating systems that I know of kill subprocesses when the parent process terminates.
However, if the process you create forks its own processes, that are not its child processes (or are detached, e.g. in the case of Windows GUI applications), those may continue running after the JVM terminates. There is an issue possibly related to that described in this question.
-
1He's talking about the Process object created via Runtime.exec which is indeed a separate process. – William Morrison Aug 13 '13 at 15:19
-
Subprocesses *are* separate processes. As I understand http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html, any `Process`, no matter how it was created, represents a subprocess of the creating process. This is necessary, in fact, to gain access to the new process's input and output streams. Check the process tree in your favorite task manager, you will see the JVM is the parent process. – Jason C Aug 13 '13 at 15:20
-
William had mentioned odd behavior with GUI applications on Windows. That is a consequence of Windows behaving "strangely", not of subprocess behavior. You can see this behavior elsewhere. For example, on Windows, start notepad.exe from a command prompt and note that you are returned to the shell immediately. If you start a console application from the prompt, it will finish executing before you are returned to the shell. This makes waiting for Windows GUI applications to finish tricky in *any* language. – Jason C Aug 13 '13 at 15:31
-
This is actually what I am doing. I am starting MS Word from Java to realise some Word to PDF conversion of user Word files. I have to shut down Word manually and it seems even then there will some resources remain after shutting down Word. – Rafael Winterhalter Aug 13 '13 at 19:41
-
1@raphw Yes, unfortunately, you will have to shut down Word manually. Windows creates GUI application processes in a detached state. I don't actually know the rationale behind that, I just know it's been that way since at least Windows 95. It's not in their CRT source either, it's something at the kernel level. If you have trouble managing Word processes, I've never tried it but you can probably use DDE to automate opening a document, converting it to a PDF, then exiting the app, and on the Java side wait for the process to finish. There is a Java-DDE library called JDDE that looks promising. – Jason C Aug 13 '13 at 19:47
-
See also http://support.microsoft.com/kb/279721 (C++, not a complete reference, but a starting point if you go the DDE route). – Jason C Aug 13 '13 at 19:48
-
See also http://poi.apache.org/ (not sure if that has PDF capabilities), or if you want to try your hand at OLE automation (poorly supported in Java) you could check out JACOB (it's old and crusty though). – Jason C Aug 13 '13 at 19:51
-
1I tried POI which does not even offer PDF conversion utilities and about any conversion utility. They all produce rather mediocre results when the documents contain more than just a few lines of text, even the comercial ones. My current solution writes Word files that I manipulated with POI to hard disk, opens Word via VBS, converts the files via VBS and shuts down Word via VBS again. When I run a test suite that starts and shuts down Word a couple of hundred times, I however remain with a couple of additional processes that do not seem to serve any purpose. Still the best I could fabricate. – Rafael Winterhalter Aug 13 '13 at 20:09
-
Gotta love it... BTW if you cannot even *kill* the processes check out SysInternals Process Explorer, you can use it to check file and library handles that a process has open, and look at stack traces (assuming debug info installed), and it might shed some light on what's keeping it from dying. Another approach might be to just open a single Word process (or a limited number) and convert all documents with that single process; possibly closing only if there is no work to be done. This would fit quite seamlessly into a worker thread + task queue model, if it fits your application. – Jason C Aug 13 '13 at 20:19
-
1That is actually what I am doing. I will ask the customer if I can open source the solution, maybe a team offer can make this fully stable on the long run. Thanks for the tip though, I'll check this out today. – Rafael Winterhalter Aug 14 '13 at 05:26
-
Unix doesn't kill child processes, it just re-parents them to the 'init' process. – user207421 Aug 14 '13 at 19:02