How do i terminate a process tree from Java?

前端 未结 5 1226
离开以前
离开以前 2021-02-05 15:26

i am using Runtime.getRuntime().exec() command in Java to start a batch file which in turn starts another process for windows platform.

javaw.exe(Process1)
 |___         


        
5条回答
  •  不思量自难忘°
    2021-02-05 16:23

    Here is another option. Use this powershell script to execute your bat script. When you want to kill the tree, terminate your powershell script's process and it will execute taskkill on it's subprocess automatically. I have it calling taskkill twice because in some cases it doesn't take on the first try.

    Param(
        [string]$path
    )
    
    $p = [Diagnostics.Process]::Start("$path").Id
    
    try {
        while($true) {
            sleep 100000
        }
    } finally {
        taskkill /pid $p
        taskkill /pid $p
    }
    

提交回复
热议问题