简体   繁体   中英

Is there a safe and correct way to kill a process tree in Windows using Java language

My situation:

There is a analysis operation in our Java EE application, this function is implemented by executing a Batch Command using java.lang.Runtime. The most serious matter is that this operation would take a long time, and during execution, some errors would be encountered, and then the whole process tree is still alive, which undoubtedly lower the performance of application and server. So I was wondering that anybody can please offer a idea to safely and correctly solve this issue.

And I know that this is a very common question in our development, I will be greatly appreciated if there is a example provided here.

If you started the Application with

Process p = Runtime.exec("");

then you can call the

p.destroy();

Other than that I think it would be great if you can make the batch process destroy itself if any error occurred in execution. Refer this question for more details.

See http://commons.apache.org/exec/apidocs/index.html

Interface CommandLauncher contains several exec methods that return Process.

But anyway you do not have any way to control the process ID: it is the OS responsibility. Moreover standard java API does not allow you even to retrieve the process ID. There was a trick in older java versions: the implementation of Process contained int field pid that could be retrieved using reflection. But this was changed in version 1.6.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM