aspose file tools*
The moose likes Java in General and the fly likes Killing process spawned by Runtime.exec() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Killing process spawned by Runtime.exec()" Watch "Killing process spawned by Runtime.exec()" New topic
Author

Killing process spawned by Runtime.exec()

J McCarch
Greenhorn

Joined: Jan 08, 2007
Posts: 8
I have a java program that spawns an external process (IE browser, actually) using the Runtime exec() method. I use Process.waitFor() so the calling java will respond appropriately when the browser is closed. However, I would like to do the converse as well - kill the browser when the java exits. The destroy() method just kills the attached java thread and doesn't affect the browser; it just continues on asynchronously.

This would be simple to resolve on UNIX, but unfortunately we are running on Windows XP. I can't find any way to determine the PID (there could be more than one IE running and I only want to kill the one spawned by the java process) to kill the browser with a system call. Does anyone have any ideas on how to resolve this?


J
Ajay Singh
Ranch Hand

Joined: Dec 13, 2006
Posts: 182
You can add shutdownHook to do that programatically.

J McCarch
Greenhorn

Joined: Jan 08, 2007
Posts: 8
Calling destroy() doesn't work; the java thread is killed but the external native process continues to run independently.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

"J Mch",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ajay Singh
Ranch Hand

Joined: Dec 13, 2006
Posts: 182
It would work, look at documentation of addShutdownHook


Give it a try
J McCarch
Greenhorn

Joined: Jan 08, 2007
Posts: 8
I tried it and the browser continued to run. The problem is the external process does not actually run in the virtual machine. I also tried it on UNIX, with which I am much more comfortable (and happy); a ps -ef showed the parent pid of the calling process until I killed the parent (programmatically so the destroy() was called), at which time it continued to run with init (PID 1) as parent. The javadoc for the Process class states that "The subprocess is not killed when there are no more references to the Process object, but rather the subprocess continues executing asynchronously".

J
Ajay Singh
Ranch Hand

Joined: Dec 13, 2006
Posts: 182
which signal did you use to kill the process. Ctrl+C works good with shutdownHook
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Just a guess, but are you starting your browser using rundll32 (a common way of asking Windows to open a URL in a browser)? If so, then the browser process isn't the process started via Runtime.exec(), so destroy() won't kill it.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
J McCarch
Greenhorn

Joined: Jan 08, 2007
Posts: 8
Do you know if a Cntrl-C can be sent via the output/input stream java classes, and what it would look like for Windows?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Offhand, I don't think you can send ctrl-C via the OutputStream. You can send the character \u0003, but the process isn't listening for it. And ctrl-C is a Unix thing anyway. I don't know offhand what the equivalent in Windows is. But maybe the thing to do here is to instead focus on how exactly you are starting this browser. I tried a simple test on my Windows 2000 box:

This seems to do exactly what it's supposed to - start up IE, wait 10 seconds, and kill it. Works fine on my box the browser exits immediately. Does it work on yours? (Modify the exacStr as necessary of course.) If not, then what OS are you using, exactly? And if it does work, then I think we need to know more about your original code, in which destroy() does not close the browser. Could you please show exactly how you're starting the browser with exec()? What's the string you're using? Peter's suggestion above is one plausible explanation for the trouble you're having, but there may be others; best to get more info before we speculate too much.
[ January 09, 2007: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Ajay Singh
Ranch Hand

Joined: Dec 13, 2006
Posts: 182

Do you know if a Cntrl-C can be sent via the output/input stream java classes, and what it would look like for Windows?

what are trying to do here? I am confused, and quite surpirsed that process.destroy doesn't work for you
Promod kumar
Ranch Hand

Joined: Jun 26, 2006
Posts: 90
I tried Jim's example and it worked for me. I am running Windows XP.
Garth Offman
Greenhorn

Joined: Jan 26, 2007
Posts: 1
I have a similar problem, however I need to invoke the Windows program via cmd.exe. I use ProcessBuilder to create a command line:



According to the thread for this bug report
Java Bug ID #4770092
process.destroy() will kill the child process (cmd.exe) but not the grandchild process (robot.exe). Is there a way around this?

I am running WinXP and jdk1.5.0_07

Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

In this particular example? Yes. Just omit the "cmd /c" part of the command line and execute "robot.exe" directly.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

What is the version of your JVM? How did you start the JVM that issues the command?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Killing process spawned by Runtime.exec()