Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中销毁进程_Java_Process - Fatal编程技术网

如何在java中销毁进程

如何在java中销毁进程,java,process,Java,Process,我写了下面的代码。要从Java应用程序运行bat文件,我使用process.exec()。但是蝙蝠可能会挂起,所以我需要为这个过程设置一个超时。我在线程中启动一个新线程和一个新进程,在线程中设置一个超时,并在超时时终止线程。但我发现当超时发生时,进程不能被破坏。所以我对如何杀死豪门女感到困惑 守则: 流氓: import java.util.*; import java.io.*; class StreamGobbler extends Thread { InputStream is

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

守则:

流氓:

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();  
              }
    }
}
主要内容:


该方法强制销毁外部进程。。。如果可能的话。(在某些情况下,您无法销毁进程,但这只是次要的。)

例如
rt.exec(“skynet”)
可能会忽略
进程。destroy()
调用。您的意思是我不能销毁进程吗?@sheng-不一定。我们所说的是,这是一种可能性。例如,如果您的应用程序在Linux/UNIX上运行setuid root进程,但它没有root权限,则无法杀死/销毁它。(Windows中可能存在类似的情况。)
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();
          }

    }
}