Runtime.getRuntime().exec()

java中用Runtime.getRuntime().exec() 调用外部程序, 获取"标准输出流", 老是阻塞. 在网上找了找, 觉得应该是"错误输出流"的问题. 果然, 为"错误输出流"单开一个线程读取之, "标准输出流"就不再阻塞了. 源码如下:

 

[java:showcolumns]  view plain copy
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. /**执行外部程序,并获取标准输出*/  
  2. public static String excuteCmd_multiThread(String[] cmd,String encoding)  
  3.     {  
  4.         BufferedReader bReader=null;  
  5.         InputStreamReader sReader=null;  
  6.         try  
  7.         {  
  8.                Process p = Runtime.getRuntime().exec(cmd);  
  9.   
  10.                /*为"错误输出流"单独开一个线程读取之,否则会造成标准输出流的阻塞*/  
  11.                Thread t=new Thread(new InputStreamRunnable(p.getErrorStream(),"ErrorStream"));  
  12.                t.start();  
  13.   
  14.                /*"标准输出流"就在当前方法中读取*/  
  15.                BufferedInputStream bis = new BufferedInputStream(p.getInputStream());  
  16.   
  17.                if(encoding!=null && encoding.length()!=0)  
  18.                {  
  19.                     sReader = new InputStreamReader(bis,encoding);//设置编码方式  
  20.                }  
  21.                else  
  22.                {  
  23.                    sReader = new InputStreamReader(bis,"GBK");  
  24.                }  
  25.                bReader=new BufferedReader(sReader);  
  26.   
  27.                StringBuilder sb=new StringBuilder();  
  28.                String line;  
  29.   
  30.                while((line=bReader.readLine())!=null)  
  31.                {  
  32.                    sb.append(line);  
  33.                    sb.append("/n");  
  34.                }  
  35.   
  36.                bReader.close();  
  37.                p.destroy();  
  38.                return sb.toString();  
  39.         }  
  40.         catch(Exception e)  
  41.         {  
  42.             e.printStackTrace();  
  43.             return ErrorString;  
  44.         }  
  45.         finally  
  46.         {  
  47.         }  
  48.     }  
  49.   
  50. /**读取InputStream的线程*/  
  51. class InputStreamRunnable implements Runnable  
  52. {  
  53.     BufferedReader bReader=null;  
  54.     String type=null;  
  55.     public InputStreamRunnable(InputStream is, String _type)  
  56.     {  
  57.         try  
  58.         {  
  59.             bReader=new BufferedReader(new InputStreamReader(new BufferedInputStream(is),"UTF-8"));  
  60.             type=_type;  
  61.         }  
  62.         catch(Exception ex)  
  63.         {  
  64.         }  
  65.     }  
  66.     public void run()  
  67.     {  
  68.         String line;  
  69.         int lineNum=0;  
  70.   
  71.         try  
  72.         {  
  73.             while((line=bReader.readLine())!=null)  
  74.             {  
  75.                 lineNum++;  
  76.                 //Thread.sleep(200);  
  77.             }  
  78.             bReader.close();  
  79.         }  
  80.         catch(Exception ex)  
  81.         {  
  82.         }  
  83.     }  
  84. }  

 

另外, Runtime.getRuntime().exec() 还有一些局限性, 就是无法像cmd那样执行较为复杂的命令. 比如, 输出流的重定向, 如:

[java]  view plain copy
  1. Runtime.getRuntime.exec("XX.exe YY.doc > ZZ.txt");  

 

他会立即返回, 不会去执行. 但是我们可以这样做, 能够完成于cmd中一样的工作:

[c-sharp]  view plain copy
  1. Runtime.getRuntime.exec("cmd /c XX.exe YY.doc > ZZ.txt");  

 

其中 /c 就是"执行后面字符串的命令". 这样就OK了,但同时还是要注意"错误输出流"的问题,依然要单开一个线程读取.否则一样会阻塞的.

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值