java 程序获取在exe 在控制台输出的信息

  public String runCommand(String cmdExp){
     InputStream[] tmpstm=new InputStream[2]; 
     String[] Info=new String[2];
     InputStreamReader isr=null;
     BufferedReader br=null;
     String okInfo="";
     String errInfo="";
     try {

         //String osName =System.getProperty("os.name");
         //System.out.println("osName:"+osName);
         String[] cmd=new String[3];
                cmd[0]="cmd.exe";
                cmd[1]="/C";
                cmd[2]=cmdExp;
         //System.out.println("正在执行的是: "+cmd[0]+" "+cmd[1]+" "+cmd[2]);
         Runtime rt=Runtime.getRuntime();
         Process proc=rt.exec(cmd);
 
         // any error message
         //gobbler errgobbler=new gobbler(proc,proc.getErrorStream(),"error");
         tmpstm[0]=proc.getInputStream();
         tmpstm[1]=proc.getErrorStream();
          // any output
         //gobbler outgobbler=new gobbler(proc,proc.getInputStream(),"output");
//        errgobbler.start();
//        outgobbler.start();
          isr=new InputStreamReader(tmpstm[0]);
          br=new BufferedReader(isr);
          String line=null;
          while((line=br.readLine())!=null){
          //  System.out.println("*******"+line);
            okInfo+=line+"\n";
            }
            
          line=null;
          isr=new InputStreamReader(tmpstm[1]);
          br=new BufferedReader(isr);
          while((line=br.readLine())!=null){
           // System.out.println("*******"+line);
            errInfo+=line+"\n";
            }
            
         //Info[0]=okInfo;
         //Info[1]=errInfo; 
         int exitVal=proc.waitFor();
         // System.out.println("exitVal "+exitVal);
     } catch (IOException e) {
             e.printStackTrace();  //To change body of catch statement use Options | File Templates.
     } catch (InterruptedException e) {
              e.printStackTrace();  //To change body of catch statement use Options | File Templates.
     }
     String mstmp="";
     if (okInfo!=null) mstmp=okInfo;
     if (errInfo!=null) mstmp+=errInfo;
     return mstmp;
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值