如何告诉 Java 运行这个 Runtime.getRuntime().exec,而不用等待它必须运行的任何命令,只需在后端运行它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12668854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:46:32  来源:igfitidea点击:

How to tell Java run this Runtime.getRuntime().exec, without waiting what ever command it has to run, simply run it in backend?

javalinuxswing

提问by David Kroukamp

How to let the Runtime.getRuntime().exec(p) run without waiting for the sleep 10?? Currently its wrong, its waiting until the exec gets complete and then moves to next. Where i need to on the fly let the exec running so that after 10 second later it can kill the PresentationInProjector.jpg.

如何让 Runtime.getRuntime().exec(p) 在不等待 sleep 10 的情况下运行??目前它是错误的,它等待 exec 完成然后移动到下一个。我需要在运行时让 exec 运行,以便在 10 秒后它可以终止 PresentationInProjector.jpg。

Example:

   Runtime.getRuntime().exec("(sleep 10; echo '09|00|CC|01|83|88' | nc localhost 58888) &");
   PlayThisSlideShow("PresentationInProjector.jpg");

回答by David Kroukamp

According to the docs exec():

根据文档exec()

Executes the specified string command in a separate process.

在单独的进程中执行指定的字符串命令。

So any call to exec()should not block unless you used waitFor()on the returned process of the Runtime.

所以任何调用exec()都不应阻塞,除非您waitFor()Runtime.

Here is a small example(Exception handling omitted):

这是一个小例子(省略了异常处理):

Process p=Runtime.getRuntime().exec("cmd.exe /c ping 127.0.0.1 -n 10");

System.out.println("Here 1");//this will execute immediately

try {

    p.waitFor();

    System.out.println("Here 2");//this will only be seen after +- 10 seconds and process has finished

} catch (InterruptedException ex) {
   ex.printStackTrace(); 
}

回答by Piotr Gwiazda

exec()is not making thread wait until spawned process ends by default. You need to call process.waitFor()explicitly to make current process wait. I guess that PlayThisSlideShow("PresentationInProjector.jpg");is being called immediately after exec(). What you see is system making JVM process be running as long as child process is running. I guess there is no way to overcome this easily, to have parent process killed while child process still running.

exec()默认情况下,不会让线程等待直到生成的进程结束。您需要process.waitFor()显式调用以使当前进程等待。我想PlayThisSlideShow("PresentationInProjector.jpg");exec(). 您看到的是,只要子进程正在运行,系统就会使 JVM 进程运行。我想没有办法轻松克服这个问题,在子进程仍在运行时杀死父进程。

Why can't you kill presentation projector from Java?

为什么不能从 Java 中杀死演示投影仪?

回答by Ian Roberts

? ?Runtime.getRuntime().exec("(sleep 10; echo '09|00|CC|01|83|88' | nc localhost 58888) &");

will not do what you expect. Runtime.execis not a shell and doesn't understand things like ()grouping, ;or |. But the actions you're trying to perform can be done purely in Java, you don't need to exec an external process. For example (exception handling omitted):

不会做你期望的。Runtime.exec不是 shell 并且不理解诸如()分组之类的东西,;或者|. 但是您尝试执行的操作可以完全在 Java 中完成,您不需要执行外部进程。例如(省略异常处理):

new Thread(new Runnable() {
  public void run() {
    Thread.sleep(10000); //sleep 10
    Socket s = new Socket("localhost", 58888); // nc
    PrintWriter pw = new PrintWriter(s.getOutputStream());
    pw.print("09|00|CC|01|83|88\n"); // echo
    pw.close();
    s.close();
  }
}).start();