由Java代码运行shell命令/脚本

   JDK有两种方法自带通Runtime.getRuntime().exec()和ProcessBuilder课上做, 后者是JDK1.5引进后,,政府还提出要放弃使用Runtime顺便做。现的时候就是採用ProcessBuilder,apache commons类库也提供了一个exec包专门做这类功能,这次临时没用到。

   在编写过程中,遇到几个比較坑的地方:

1、构建ProcessBuilder採用的參数:

    建议採用“/bin/bash”. "-c",  "your shell"组装一个List。 事实上你一次假设运行多个命令。都能够统一放到那个“your shell”字符串中。

2、运行过程中输出流控制:

   在运行过程中,我们肯定须要得到正常的运行结果。也须要知道出错的内容提示。 这时须要将错误输出流重定向到标准输出流,相当于合并在一起输出

3、有些命令是须要环境变量的支持, 这时须要运行evivonment()拷贝系统相关env变量到当前进程上下文中。供命令使用。

4、假设运行过程卡死,须要知道能够kill哪个进程, 所以输出内容中将当前进程ID打印出,便于手动处理。


完整代码例如以下:

  public int exeCmd(String shell) throws IOException {
    int success = 0;
    StringBuffer sb = new StringBuffer();
    BufferedReader br = null;

    // get name representing the running Java virtual machine.
    String name = ManagementFactory.getRuntimeMXBean().getName();
    String pid = name.split("@")[0];

    try {
      System.out.println("Starting to exec{ " + shell + " }. PID is: " + pid);
      Process process = null;
      ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", shell);
      pb.environment();
      pb.redirectErrorStream(true); // merge error stream into standard stream
      process = pb.start();
      if (process != null) {
        br = new BufferedReader(
            new InputStreamReader(process.getInputStream()), 1024);
        process.waitFor();
      } else {
        System.out.println("There is no PID found.");
      }
      sb.append("Ending exec right now, the result is:
");
      String line = null;
      while (br != null && (line = br.readLine()) != null) {
        sb.append(line).append("
");
      }
    } catch (Exception ioe) {
      sb.append("Error occured when exec cmd:
").append(ioe.getMessage())
          .append("
");
    } finally {
      PrintWriter writer = null;
      if (br != null) {
        br.close();
      }
      try {
        writer = new PrintWriter(System.out);
        writer.write(sb.toString());
      } catch (Exception e) {
        LOG.error(e.getMessage(), e);
      } finally {
        writer.close();
      }
      success = 1;
    }
    return success;
  }


版权声明:本文博客原创文章。博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/blfshiye/p/4689833.html