首页 > 编程语言 > 详细

JAVA执行SHELL脚本

时间:2021-05-23 17:08:59      阅读:21      评论:0      收藏:0      [点我收藏+]

/**

 *@Description //执行shell脚本

 *@Param [cmd]

 *@Return boolean

 */

public static boolean execSchellCmd(Stringcmd[]) throws Exception{

  LineNumberReader lnr = null;

  InputStreamReader isr = null;

  try{

    //解决脚本没有执行权限

    ProcessBuilder builder = new Processbuilder("/bin/chmod","755",cmd[0]);

    Process ps = builder.start();

    ps.waitFor();

 

    //执行指定的字符串命令

    Process process = Runtime.getRuntime().exec(cmd);

    //获取子进程正常输出的输入流

    isr = new InputStreamReader(process.getInputStream());

    lnr = new LineNumberReader(isr);

    String line;

    StringBuffer sb = new StringBuffer();

    //逐行读取文本行

    while((line = lnr.readLine()) !=null){

      sb.append("\n").append(line);

    }

    sb.append("\n").append("----------------------");

    //等待进程执行完成,如返回0,表示正常完成,否则表示异常

    return process.waitFor()==0;

  }fianlly{

    if(lnr!=null){

      lnr.close();

      lnr = null;

    }

    if(isr != null){

      isr.close();

      isr = null;

    }

  }

}

 

---------------------------------------------------------------------------调用------------------------------------------------------------------------------------

//拼接执行命令

String[] cmd new String[]{"/home/ap/hab8haap/POSQSCP.sh"};

String param = "20210556";

cmd = ArrayUtils.addAll(cmd,param);

//执行shell

boolean execschellflag = execSchellCmd(cmd);

 

JAVA执行SHELL脚本

原文:https://www.cnblogs.com/cjl-lhj/p/14801370.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!