java – 运行时 Runtime.getRuntime().exec 挂起 一直加载中 执行阻塞问题,需获取执行结果

使用Runtime.getRuntime().exec执行Shell
执行命令时出现问题, 应用程序会挂起 并没有执行,也没有结果

		StringBuffer result = new StringBuffer();

        try {
            Process proc = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", execute});
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            if (returnOne) {
                if ((line = in.readLine()) != null) {
                    result.append(line);
                }
            } else {
                while((line = in.readLine()) != null) {
                    result.append(line);
                }
            }

            System.out.println("执行Shell脚本 结束", line);
            in.close();
            int exitValue = proc.waitFor();
            if (0 != exitValue) {
                log.error("执行Shell脚本失败. error code is :" + exitValue);
            }
        } catch (IOException var8) {
            var8.printStackTrace();
            log.error("执行Shell脚本失败 {}", execute, var8);
        } catch (InterruptedException var9) {
            var9.printStackTrace();
            log.error("执行Shell脚本失败~  {}", execute, var9);
        }

查了资料才指定Runtime.getRuntime().exec 同时产生InputStream,ErrorStream,2个流是并行。 当前从输出流传输的数据填满了操作系统缓冲区,您的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 ();
        }
    }
}

最终代码改成

StringBuffer result = new StringBuffer();

        try {
            Process proc = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", execute});
            ReadStream s2 = new ReadStream("执行Shell脚本错误", proc.getErrorStream());
            s2.start();
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            if (returnOne) {
                if ((line = in.readLine()) != null) {
                    result.append(line);
                }
            } else {
                while((line = in.readLine()) != null) {
                    result.append(line);
                }
            }

            System.out.println("执行Shell脚本 结束", line);
            in.close();
            int exitValue = proc.waitFor();
            if (0 != exitValue) {
                log.error("执行Shell脚本失败. error code is :" + exitValue);
            }
        } catch (IOException var8) {
            var8.printStackTrace();
            log.error("执行Shell脚本失败 {}", execute, var8);
        } catch (InterruptedException var9) {
            var9.printStackTrace();
            log.error("执行Shell脚本失败~  {}", execute, var9);
        }
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值