【问题标题】:runtime exec commands do not execute successively运行时 exec 命令不连续执行
【发布时间】:2013-11-25 20:40:50
【问题描述】:

我正在尝试使用 runtime exec 连续运行各种命令。我创建了 getRuntime 方法的一个实例,并使用同一个实例连续调用不同的命令,但它们都同时执行。如果运行时 exec 没有阻塞,那么在第一个命令完成后执行第二个命令的好方法是什么?

Runtime runTime = Runtime.getRuntime();

runTime.exec(new String[]{"sh", "-c", "some command"});
runTime.exec(new String[]{"other command"});
runTime.exec(new String[]{"sh","-c","final command"});

【问题讨论】:

  • 请注意,如果您没有正确读取输出流,某些进程会阻塞...
  • 我的答案是正确的替代方法。你为什么投反对票?

标签: java


【解决方案1】:

我会使用waitFor

runTime.exec(new String[]{"sh", "-c", "some command"}).waitFor();
runTime.exec(new String[]{"other command"}).waitFor();
runTime.exec(new String[]{"sh","-c","final command"}).waitFor();

【讨论】:

    【解决方案2】:

    你需要使用waitFor():

    Process p = runTime.exec(...)
    int exitValue = p.waitFor()
    System.out.println("Exit value:  " + exitValue;
    

    洗涤漂洗重复

    【讨论】:

      猜你喜欢
      • 2013-09-07
      • 1970-01-01
      • 2017-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多