• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Killing process spawned by Runtime.exec()

 
J McCarch
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can add shutdownHook to do that programatically.

 
J McCarch
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would work, look at documentation of addShutdownHook


Give it a try
 
J McCarch
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which signal did you use to kill the process. Ctrl+C works good with shutdownHook
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
J McCarch
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried Jim's example and it worked for me. I am running Windows XP.
 
Garth Offman
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the version of your JVM? How did you start the JVM that issues the command?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic