如何在java中销毁进程?

时间:2021-08-08 19:35:13

I wrote the code below. To run a bat file from Java app, I use a process.exec(). But the bat may hang sometime, so I need to set a timeout for this process. I start a new thread and new a process in the thread, I set a timeout in the thread, and kill the thread when it is timeout. But I found that the process couldn't be destroyed when timeout happens. So I am confused about how to kill the porcess?

我写了下面的代码。要从Java应用程序运行bat文件,我使用process.exec()。但是蝙蝠可能会挂起,所以我需要为这个过程设置一个超时。我在线程中启动一个新线程和一个新进程,在线程中设置一个超时,并在线程超时时终止线程。但是我发现当超时发生时,进程不能被破坏。所以我对如何杀死这个过程感到困惑?

The code:

代码:

StreamGobbler:

StreamGobbler:

import java.util.*;

import java.io.*;

class StreamGobbler extends Thread
{
    InputStream is;
    String type;

    StreamGobbler(InputStream is, String type)
    {
        this.is = is;
        this.type = type;
    }

    public void run()
    {
        try
        {
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line=null;
            while ( (line = br.readLine()) != null)
                System.out.println(type + ">" + line);    
            } catch (IOException ioe)
              {
                ioe.printStackTrace();  
              }
    }
}

Main:

主要:

public class test
{

    public static void main(String args[]) throws InterruptedException

    {
        Runnable r = new ShengThread();
        Thread sheng = new Thread(r);
        sheng.start();
        sheng.join(1000);
        if (sheng.isAlive()) {
            sheng.interrupt();
        }

        if (sheng.isAlive()) {
            System.out.println("It is dead.");
        }



    }
}

class ShengThread implements Runnable {

    public void run() {

        Process proc = null;

        try
        {            
            String osName = System.getProperty("os.name" );
            String[] cmd = new String[3];
            if( osName.equals( "Windows XP" ) )
            {
                cmd[0] = "cmd" ;
                cmd[1] = "/C" ;
                cmd[2] = "c:\\status.bat";
            }


            Runtime rt = Runtime.getRuntime();

            System.out.println(osName+"Execing " + cmd[0] + " " + cmd[1] 
                               + " " + cmd[2]);
            try {
                proc = rt.exec(cmd);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // any error message?
            StreamGobbler errorGobbler = new 
                StreamGobbler(proc.getErrorStream(), "ERROR");            

            // any output?
            StreamGobbler outputGobbler = new 
                StreamGobbler(proc.getInputStream(), "OUTPUT");

            // kick them off
            errorGobbler.start();
            outputGobbler.start();

            // any error???
            int exitVal = proc.waitFor();
            System.out.println("ExitValue: " + exitVal);        
        } catch (InterruptedException t)
          {
            System.out.println("start\n");
            proc.destroy();
            t.printStackTrace();
          }

    }
}

1 个解决方案

#1


5  

The Process.destroy() method forcibly destroys an external process ... if this is possible. (In some situations you can't destroy processes, but that's only marginally relevant.)

销毁()方法强制销毁外部进程…如果这是可能的。(在某些情况下,你不能破坏流程,但这只是略微相关而已。)

#1


5  

The Process.destroy() method forcibly destroys an external process ... if this is possible. (In some situations you can't destroy processes, but that's only marginally relevant.)

销毁()方法强制销毁外部进程…如果这是可能的。(在某些情况下,你不能破坏流程,但这只是略微相关而已。)