`
JasonShieh
  • 浏览: 521111 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Runtime.getRuntime().exec执行阻塞问题解决 .

    博客分类:
  • JAVA
 
阅读更多
转自:http://blog.csdn.net/dysj4099/article/details/5985596
当cmd命令执

行出现错误或警告时,主控程序的waitfor方法会被阻塞一直等待下去,查了查资料发现是Runtime.getRuntime().exec方法需要自己处理



stderr 及stdout流,而解决方法即是将它们导出用别的thread处理。



会造成阻塞的代码:



view plaincopy to clipboardprint?
01.Process p = Runtime.getRuntime().exec(cmd); 
02. 
03.p.waitFor(); 
Process p = Runtime.getRuntime().exec(cmd);

p.waitFor();



解决方法:

view plaincopy to clipboardprint?
01.Process p = Runtime.getRuntime().exec(cmd);  
02.  
03.StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");              
04.        
05.      // kick off stderr   
06.      errorGobbler.start();  
07.        
08.      StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(), "STDOUT");  
09.      // kick off stdout   
10.      outGobbler.start();   
11.  
12.p.waitFor();  
			Process p = Runtime.getRuntime().exec(cmd);
			
			StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");            
	        
	        // kick off stderr
	        errorGobbler.start();
	        
	        StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(), "STDOUT");
	        // kick off stdout
	        outGobbler.start(); 
			
			p.waitFor(); 

 


其中StreamGobbler类的代码:


view plaincopy to clipboardprint?
01.package com.sdc.callmaxent.socket;  
02.  
03.import java.io.BufferedReader;  
04.import java.io.IOException;  
05.import java.io.InputStream;  
06.import java.io.InputStreamReader;  
07.import java.io.OutputStream;  
08.import java.io.PrintWriter;  
09.  
10.import com.sdc.callmaxent.util.FileUtil;  
11.  
12./** 
13. * 用于处理Runtime.getRuntime().exec产生的错误流及输出流 
14. * @author shaojing 
15. * 
16. */  
17.public class StreamGobbler extends Thread {  
18.    InputStream is;  
19.    String type;  
20.    OutputStream os;  
21.          
22.    StreamGobbler(InputStream is, String type) {  
23.        this(is, type, null);  
24.    }  
25.  
26.    StreamGobbler(InputStream is, String type, OutputStream redirect) {  
27.        this.is = is;  
28.        this.type = type;  
29.        this.os = redirect;  
30.    }  
31.      
32.    public void run() {  
33.        InputStreamReader isr = null;  
34.        BufferedReader br = null;  
35.        PrintWriter pw = null;  
36.        try {  
37.            if (os != null)  
38.                pw = new PrintWriter(os);  
39.                  
40.            isr = new InputStreamReader(is);  
41.            br = new BufferedReader(isr);  
42.            String line=null;  
43.            while ( (line = br.readLine()) != null) {  
44.                if (pw != null)  
45.                    pw.println(line);  
46.                System.out.println(type + ">" + line);      
47.            }  
48.              
49.            if (pw != null)  
50.                pw.flush();  
51.        } catch (IOException ioe) {  
52.            ioe.printStackTrace();    
53.        } finally{  
54.            FileUtil.close(pw);  
55.            FileUtil.close(br);  
56.            FileUtil.close(isr);  
57.        }  
58.    }  
59.}   
分享到:
评论
2 楼 xblia 2011-09-16  
xblia 写道
   

  
1 楼 xblia 2011-09-16  
   

相关推荐

    Runtime 执行bat

    Runtime 执行bat

    解决runtime.exec()执行进程block死锁以及为waitFor设置超时

    完美解决runtime.exec()执行进程block死锁以及为waitFor设置超时 不需要耗cpu的循环判断exitValue==0 开两个进程搞定

    Android中软件的静默安装

    1,申请root权限Runtime.getRuntime().exec("su"); 2,通过数据输出流DataOutputStream写入pm install命令; 3,最后获取Process进程的返回值int i = process.waitFor();,如果i=0,则表明已获取root权限。

    【IDEA】windows环境下IDEA java代码Runtime.getRuntime.exec中shell的执行环境的解决方案

    windows环境下IDEA java代码Runtime.getRuntime.exec中shell的执行环境的解决方案前言解决办法后记 前言 在使用IDEA本地开发监控守护线程的后台,我遇上了执行环境不兼容的问题,爆出各种“xxx不是内部或外部命令,...

    详解Java8与Runtime.getRuntime().availableProcessors()

    主要介绍了详解Java8与Runtime.getRuntime().availableProcessors(),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    android截屏

    这里不是通过view来截图,也不是通过底层的framebuffer实现截图,而是采用另外一种方法实现截图,通过Runtime.getRuntime().exec()来实现,并保存在sdcard上,代码很简单。

    AIUI使用.rar

    runtime.exec("cmd /c start " + url); } catch (IOException e) { e.printStackTrace(); } } /** * 鍦ㄥ欢杩熸寚瀹氱殑绉掓暟鍚庡叧鏈? * * @param sec * 鍏虫満寤惰繜锛屽崟浣嶏細绉? */ ...

    解决JVM实际使用的内存比-Xmx的少的问题.docx

    System.out.println("Runtime.getRuntime().maxMemory()="+Runtime.getRuntime().maxMemory()); 而且确实,现有检测工具底层也是用这个语句来进行检测。要解决这个问题,首先我们需要一个可重复使用的测试用例。因此...

    Delphi实现android系统的步进电机控制.rar

     //Process p = Runtime.getRuntime().exec("su");  //然后,在向这个进程的写入要执行的命令,即可达到以root权限执行命令:  //dos.flush();  //或者用下面的方式:  //Runtime.getRuntime().exec&#...

    Java调用Linux命令

    (注意:Runtime.getRuntime().exec(command)返回的是一个Process类的实例), 该实例可用于控制进程或取得进程的相关信息. 由于调用Runtime.exec方法所创建的子进程没有自己的终端或控制台,因此该子进程的标准IO...

    Java编程使用Runtime和Process类运行外部程序的方法

    主要介绍了Java编程使用Runtime和Process类运行外部程序的方法,结合实例形式分析了java使用Runtime.getRuntime().exec()方法运行外部程序的常见情况与操作技巧,需要的朋友可以参考下

    runtimepermission

    动态权限工具类

    java实现动态波形曲线显示.rar

     java的Runtime.getRuntime().exec(commandStr)可以调用执行cmd指令。  cmd /c dir 是执行完dir命令后关闭命令窗口。  cmd /k dir 是执行完dir命令后不关闭命令窗口。  cmd /c start dir 会打开一个新...

    使用JAVA获取客户端MAC地址.doc

    利用Runtime call操作系统的命令,具体的命令取决于不同的操作系统,注意不要调用Runtime.getRuntime().exec(String)接口,要用Runtime.getRuntime().exec(String[])这个接口,不然复杂命令的执行会有问题。...

    echarts-convert.zip

    java用Runtime.getRuntime().exec(cmd)调用js即可,

    Java使用默认浏览器打开指定URL的方法(二种方法)

    直接看代码:方法一: 代码如下:Runtime.getRuntime().exec(“rundll32 url.dll,FileProtocolHandler //www.jb51.net”); 方法二: 代码如下://判断当前系统是否支持Java AWT Desktop扩展 if(java.awt....

    安卓程序发送linux指令.zip

    Runtime.getRuntime().exec("sh")以及linux echo写入指令

    java 查看任务管理里面的所有线程

    java 查看任务管理里面的所有线程 Proces java.lang.Runtime.getRuntime().exec("ipconfig");

    java修改文件属性

    所以我们必须到Dos环境下去设置,在java中用Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath()+ """+ " +R")该方法可以实现。因为路径file.getAbsolutePath()中可能会还有空格,所以必须...

Global site tag (gtag.js) - Google Analytics