Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Kill a process tree" on windows using Java

I have a Java webstart process that is part of a windows batch script. I'm using the javaws command in a batch script in this case. This match script ( start.bat) is invoked programatically using the "apache commons exec". Under some conditions the java process invoked by javaws hangs and I'd have to kill the entire process thread starting from the batch script start.bat. Is there a programatic way of doing killing an entire process tree through apache commons exec?

I've tried using the "execWatchdog.destroyProcess();" on the "start.bat" script. However it only kills the start.bat process and not the entire process tree.

Is there a way of killing the entire process tree through apache-commons-exec or a similar code?

I've seen this question Performing equivalent of "Kill Process Tree" in c++ on windows that performs an equivalent task in c++. I'm wondering if anyone has implemented calling windows native system calls through JNI.

like image 754
Rajesh Kazhankodath Avatar asked Sep 26 '11 03:09

Rajesh Kazhankodath


People also ask

How do I kill a specific process in Windows java?

So by using the java process name you are now able to kill the desired process. This command will execute the “jps -m” command and get the PID of the java processes which contain the given “JAVA_PROCESS_NAME” and execute a “taskkill /F /PID” over. N.B.1: replace the “JAVA_PROCESS_NAME” by your own process name.

How do you kill a process tree?

Using htop , you can use F5 to show the process tree's. If you select the process at the top of the tree you want kill, then press F9 followed by Enter it will close the process and the entire process tree in one go. In the screen shot below this action would cause Chrome and all sub process to be closed.

How do you kill a process in java?

All you need to do is press "k" on the keyboard and enter the process's id that you want to kill. The ps command also displays information about the processes similar to the "top" command that we have used earlier. However, it does not provide a similar interface as the "top" command does.


1 Answers

Finally got something workable even though its a roundabout way.

Apache Commons Exec API contains the CommandLauncher class that returns a java.lang.Process object. Thanks to the link

Here the link to get the windows Process Id from a java.lang.Process. This uses the JNA libraries.

Finally with the Process Id, here the command string that kills the process tree //String killCmd = "taskkill /F /T /PID " + JNAHandler.getPid(process);

like image 52
Rajesh Kazhankodath Avatar answered Oct 01 '22 21:10

Rajesh Kazhankodath