-   Programming (
-   -   process and child process in java (

xhi 03-27-2006 01:37 PM

process and child process in java
ok what i have is a process in java that is created and then has multiple commands written to it using a stream. so as this process gets written to it spawns child processes, and some of those child processes are large. some taking up to a half hour to run. i need to implement a method that will kill the current running child process (basically a cancel button). however according to java

The subprocess is not killed when there are no more references to the Process object, but rather the subprocess continues executing asynchronously.
so it would seem that there is no way to keep track of a child process. however, when the java application is closed the child process is killed. so there is somthing in there that knows about the child process, it just seems like i cant get to it.

i know the ideal method would be to launch the child in its own process, however the system is already built and i do not really want to change more than i have to. so any ideas on killing this child process would be welcomed..

oh and this is in windows.

95se 03-27-2006 04:33 PM

What it means by that is that even if there are no more references to a process somewhere, it won't be stopped. I think it's just referring to a frequent question people may have concerning Java's garbage collection and the Process class. Typically, if there are no more references to an object in Java, that object is removed to free up memory. This is not true for the Process object, since a Process object, even if there are no more references to it, may still be doing useful work. If Java exits, it will kill all Process I guess, but if your program is still running, and the Process hasn't exited normally yet, and you haven't killed it, then it will stay alive (even if there are no references to it anywhere). This does not mean you can't kill it. Use the destroy method,


public abstract void destroy()

Kills the subprocess. The subprocess represented by this Process object is forcibly terminated.

95se 03-27-2006 04:38 PM

Hrmmm, I get what your saying now, nvm.

xhi 03-27-2006 05:07 PM


Hrmmm, I get what your saying now, nvm.
yeh.. actually after i had someone else here read the quote i posted i saw that it is not exactly a good example of what i am referring to..

anyhow, it now appears from some other reasearch, that once a child process is spawned from the current process, java does not track that sub process at all.. and even if i kill the process that i write the commands to, it still does not kill the child.. apparently for portability reasons they did not included any way to track child processes..

so i am going to end up rewriting a bunch of other code to reimplement this.. unless someone has a smart way to do this.. it was suggested to me to parse the pid out of a task list and kill that task.. however this seems even less portable than using Process..

xhi 03-28-2006 10:36 AM

ok.. well this is for the most part taken care of now. i ended up using the common method of just writing to the stream that process provides..

however now i have another problem. when this external program is inside a large loop, i cannot interrupt it by writing a ctrl-c to the process. however when i run it from console (not java), i can ctrl-c it anytime and it responds immediately..

i have no prior experience running this large of external program from java and i wonder if there are some little gotchas that i dont know about.. it would seem that the ?thread? that the external program is running in is so high priority that it will not respond to the ctrl-c.. however i do not see anyway to change the priority as it would be the OS that is controlling the external program.. ??


All times are GMT -5. The time now is 05:59 AM.