【问题标题】:How to start a program and exit immediately using apache commons-exec?如何使用 apache commons-exec 启动程序并立即退出?
【发布时间】:2012-11-28 10:12:57
【问题描述】:

通过使用 Apache Commons-Exec 我可以成功启动程序,但是启动程序在程序启动后暂停。

请问如何启动一个程序并立即退出,才不会阻塞后面的执行?

DefaultExecutor executor = new DefaultExecutor();
executor.execute( "cmd /c start C:\\Users\\xx\\program.exe");

我在 Win7 64 位。

谢谢

【问题讨论】:

    标签: java apache-commons-exec


    【解决方案1】:

    根据documentationexecute(CommandLine)开始同步执行。也就是说,它阻塞了调用线程。你可能想要异步执行,所以使用execute(CommandLine command, ExecuteResultHandler handler)。例如,

    DefaultExecutor executor = new DefaultExecutor();
    executor.execute(new CommadLine("cmd /c start C:\\Users\\xx\\program.exe"),
                     new DefaultExecuteResultHandler());
    

    【讨论】:

      【解决方案2】:

      听起来您需要使用DefaultExecuteResultHandler

      查看tutorial(底部)了解更多信息。

      【讨论】:

      • 好吧,我修改了 apache 的教程,设置了一个看门狗,比如 3 秒。该程序将执行另一个 java 应用程序,它是一个线程,将在 10 秒后停止运行。问题是,直到子进程在 10 秒内关闭,父进程才会关闭。看门狗不工作。父程序是异步的。任何进一步的帮助,.?谢谢。
      • 我在 Linux 上尝试运行 *.sh 文件时看到了相同的行为(第一行是 #!/bin/sh)。看门狗在超时后不会停止这个过程! :(
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多