Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.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 Runtime.exec()挂起_Java_Runtimeexception - Fatal编程技术网

Java Runtime.exec()挂起

Java Runtime.exec()挂起,java,runtimeexception,Java,Runtimeexception,每当执行下面的代码时,我就会进入一个已失效的僵尸进程。有人能帮我解决这个问题吗 private static boolean executeCommand(String command) throws ClientException, IOException, InterruptedException { int exitVal = 1; // 0 is success, so we default to a nonzero. Process proc =

每当执行下面的代码时,我就会进入一个已失效的僵尸进程。有人能帮我解决这个问题吗

  private static boolean executeCommand(String command)
        throws ClientException, IOException, InterruptedException {

    int exitVal = 1; // 0 is success, so we default to a nonzero.
    Process proc = null;
    try{
    Runtime rt = Runtime.getRuntime();
    proc = rt.exec(command);

    //Below lines are required to flush out the streams. else the process will hang.
    ReadStream s1 = new ReadStream("stdin", proc.getInputStream ());
    ReadStream s2 = new ReadStream("stderr", proc.getErrorStream ());
    s1.start ();
    s2.start ();        

    exitVal = proc.waitFor();

    if (exitVal == 0) {
        return true;
    } else {
        throw new ClientException("103", "" + command + " failed.");
    }
    }finally{
        if(proc  != null){
            proc.destroy();
        }
    }

}
我正在以不同的方式清理所有的溪流

这是我的ReadStream类

 public class ReadStream implements Runnable {

private static Logger logger = Logger.getLogger(ReadStream.class);

String name;
InputStream is;
Thread thread;

public ReadStream(String name, InputStream is) {
    this.name = name;
    this.is = is;
}

public void start() {
    thread = new Thread(this);
    thread.start();
}

public void run() {
    try {
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        while (true) {
            String s = br.readLine();
            if (s == null)
                break;
            logger.info("[" + name + "] " + s);
        }
        is.close();
    } catch (Exception ex) {
        logger.error("Problem reading stream " + name + "... :" + ex);

    }
}

}

我认为这不是问题所在,但请尝试将运行方法更改为:

try {
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        do {
            String s = br.readLine();
            if (s != null)
              logger.info("[" + name + "] " + s);
        } while (s != null);
        is.close();
    } catch (Exception ex) {
        logger.error("Problem reading stream " + name + "... :" + ex);

    }

你在传递什么命令?ReadStream看起来像什么?谢谢Dukeling。。。。您好,我在问题中更新了ReadStream。另外,我使用的是mv命令,在调用
waitFor()
之前,您需要关闭进程的输入流,这是通过
process.getOutputStream()
获得的,这很令人困惑,而且您应该只在
waitFor()
失败时调用
process.destroy()
。嗨,EJP,我不明白您在第一行中的意思。你能写一些代码片段吗?嗨,但是你认为是什么导致创建了这个不存在的进程?有什么想法吗?你应该在你的日志中记录你在那里收到了什么,只要你没有得到null返回它的infite循环。