编程语言
首页 > 编程语言> > java – 运行时.getRuntime().exec(cmd)挂起

java – 运行时.getRuntime().exec(cmd)挂起

作者:互联网

我正在执行一个命令,它返回一个文件的修订号; ‘文档名称’.但是如果执行命令时出现问题,则应用程序会挂起.我该怎么做才能避免这种情况?请在下面找到我的代码.

String cmd= "cmd /C si viewhistory --fields=revision --project="+fileName; 
Process p = Runtime.getRuntime().exec(cmd) ;  
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));  
String line = null; 
while ((line = in.readLine()) != null) {  
System.out.println(line);  
} 

} catch (Exception e) {  
e.printStackTrace();  
 }

解决方法:

我想问题是你只是在阅读InputStream而不是读ErrorStream.您还必须注意并行读取两个流.可能会发生这样的情况:当前从输出流传输的数据填满了操作系统缓冲区,您的exec命令将自动暂停,以便读者有机会清空缓冲区.但该程序仍将等待输出处理.因此,发生了挂起.

您可以创建一个单独的类来处理输入和错误流,如下所示,

public class ReadStream implements Runnable {
    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;
                System.out.println ("[" + name + "] " + s);
            }
            is.close ();    
        } catch (Exception ex) {
            System.out.println ("Problem reading stream " + name + "... :" + ex);
            ex.printStackTrace ();
        }
    }
}

你使用它的方式如下,

String cmd= "cmd /C si viewhistory --fields=revision --project="+fileName; 
Process p = Runtime.getRuntime().exec(cmd) ;  
s1 = new ReadStream("stdin", p.getInputStream ());
s2 = new ReadStream("stderr", p.getErrorStream ());
s1.start ();
s2.start ();
p.waitFor();        
} catch (Exception e) {  
e.printStackTrace();  
} finally {
    if(p != null)
        p.destroy();
}

标签:runtime-exec,processbuilder,java,process
来源: https://codeday.me/bug/20190926/1818274.html