Runtime.getRuntime()。exec(cmd)挂起

时间:2012-10-22 09:34:35

标签: java process runtime.exec processbuilder

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

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();  
 }

2 个答案:

答案 0 :(得分:29)

我想问题是你只是在阅读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();
}

答案 1 :(得分:4)

此代码基于Arham的答案,但使用java 8并行流实现,这使其更简洁。

public static String getOutputFromProgram(String program) throws IOException {
    Process proc = Runtime.getRuntime().exec(program);
    return Stream.of(proc.getErrorStream(), proc.getInputStream()).parallel().map((InputStream isForOutput) -> {
        StringBuilder output = new StringBuilder();
        try (BufferedReader br = new BufferedReader(new InputStreamReader(isForOutput))) {
            String line;
            while ((line = br.readLine()) != null) {
                output.append(line);
                output.append("\n");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return output;
    }).collect(Collectors.joining());
}

你可以这样调用这个方法

getOutputFromProgram("cmd /C si viewhistory --fields=revision --project="+fileName);

请注意,如果您要调用的程序挂起,则此方法将挂起,如果需要输入,则会挂起。