Home arrow static arrow Java Programming [Archive] - How to kill subprocesses spawned by a process started by Process.exec()?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - How to kill subprocesses spawned by a process started by Process.exec()?
This topic has 5 replies on 1 page.

Posts:5
Registered: 5/2/00
How to kill subprocesses spawned by a process started by Process.exec()?  
Jul 26, 2004 1:27 AM



 
I have a java code which goes into infinite loop.

public class Waiter {
public static void main(String[] args) {
long i = 0;
while (true) {
i++;
System.out.println("Sleeping one sec now: "+i);
try {
Thread.sleep(1000);
}
catch (Exception e) {
System.out.println("Interrupted!");
}
}
}
}

A batch file, batch.bat, inovokes this.

SET CLASSPATH=%CLASSPATH%;.
java Waiter

Another java program uses Process.exec to call this batch file.

public class Invoker {
public static void main(String[] args) throws Exception {
Process p = Runtime.getRuntime().exec("batch.bat");
StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), "OUT");
StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERR");
outputGobbler.start();
errorGobbler.start();
Thread.sleep(5000);
p.destroy();
System.out.println("Destroy called...." + p.waitFor());
outputGobbler.interrupt();
errorGobbler.interrupt();
}
}

Now, the problem I am facing is that process.destroy obviously kills the batch process running, but, does not kill the subprocesses spawned by it, Waiter class in the case.

Can anybody help me on this, so that I can even kill the Waiter class from the Invoker?

Regards,
Vaibhave Kuchhal
vaibhav.kuchhal@iflexsolutions.com
 

Posts:10
Registered: 7/25/04
Re: How to kill subprocesses spawned by a process started by Process.exec()?  
Jul 26, 2004 1:50 AM (reply 1 of 5)



 
Hi I am also looking for a solution to this problem. On a side note, how are you getting process.destroy() to work before closing the streams?
Thanks.
 

Posts:5
Registered: 5/2/00
Re: How to kill subprocesses spawned by a process started by Process.exec()?  
Jul 26, 2004 1:54 AM (reply 2 of 5)



 
I have not opened any IO stream in Waiter.java. It's Invoker.java that has the open streams.
 

Posts:10
Registered: 7/25/04
Re: How to kill subprocesses spawned by a process started by Process.exec()?  
Jul 26, 2004 2:03 AM (reply 3 of 5)



 
In invoker.java I thought it was necessary to do process.getErrorStream().close() and getInputStream().close() before calling process.destroy();
 

Posts:10
Registered: 7/25/04
Re: How to kill subprocesses spawned by a process started by Process.exec()?  
Jul 26, 2004 2:05 AM (reply 4 of 5)



 
Do you think its necessary to spawn native processes using the Native Interface to fix your problem? Get the process ID's and kill them?
 

Posts:5
Registered: 5/2/00
Re: How to kill subprocesses spawned by a process started by Process.exec()?  
Jul 26, 2004 2:13 AM (reply 5 of 5)



 
I think, you did not get my concern. Let me explain it again.

I have a java program, Invoker.java, which calls upon batch.bat using process.exec(). This batch.bat starts another java program, Waiter.java, which goes into infinite loop. Now, if I call process.destroy(), it terminates batch.bat, but, does not kill Waiter. It is quite obvious.

I want to know a way so that I can even kill Waiter class running from Invoker.

Regards,
Vaibhave
 
This topic has 5 replies on 1 page.