zoukankan      html  css  js  c++  java
  • Java调用shell脚本

    最近的新项目有多个地方需要调用shell脚本,这里记录下简单的shell脚本调用方法。代码如下:

    private void callSh() {
      InputStreamReader stdISR = null; 
            InputStreamReader errISR = null; 
            Process process = null;
      //调用的脚本及路径
      String command = "/home/mw/weblogic/test.sh"; 
      try {
       process = Runtime.getRuntime().exec(command);
       BufferedReader stdBR = new BufferedReader(new InputStreamReader(process.getInputStream()));
       BufferedReader errBR = new BufferedReader(new InputStreamReader(process.getErrorStream()));
             String line = ""; 
             while ((line = stdBR.readLine()) != null) { 
                 System.out.println("STD line:" + line); 
             }
       
       while ((line = errBR.readLine()) != null) { 
                 System.out.println("ERR line:" +line); 
             }
            
      } catch (Exception e) {
       throw new BusinessException("执行脚本失败===="+e);
      }finally{
       if(stdBR != null){
        stdBR.close(); 
       }
       if(errBR != null){
        errBR.close();
       }
       if(process != null){
        process.destroy();
       }
       
      }
      
     }

    此代码只适用一般的shell脚本调用,如果shell脚本内容比较多,语法比较复杂,因为没有很好的容错机制,使用此方式可能就会出现问题。这里看过一篇文章,可借鉴:

    http://blog.csdn.net/lance_wyvern/article/details/50456903#comments

  • 相关阅读:
    go---weichart个人对Golang中并发理解
    go语言值得学习的开源项目推荐
    mysql17---增量备份
    mysql16---读写分离
    mysql15--垂直分表水平分表
    mysql14---手动备份
    mysql13---索引使用注意
    mysql12----explain
    mysql11---主键普通全文索引
    OpenOffice的简单安装
  • 原文地址:https://www.cnblogs.com/runnigwolf/p/7615775.html
Copyright © 2011-2022 走看看