如何告诉Java运行此Runtime.getRuntime()。exec,而不等待它必须运行的命令,只需在后端运行它? [英] How to tell Java run this Runtime.getRuntime().exec, without waiting what ever command it has to run, simply run it in backend?

查看:289
本文介绍了如何告诉Java运行此Runtime.getRuntime()。exec,而不等待它必须运行的命令,只需在后端运行它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让Runtime.getRuntime()。exec(p)在不等待睡眠的情况下运行10 ??
目前是错误的,等到exec完成然后转到下一个。我需要在运行中让exec运行,以便在10秒后它可以杀死PresentationInProjector.jpg。

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.

Example:

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


推荐答案

根据文档 exec( )


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

Executes the specified string command in a separate process.

因此,除非您使用,否则不应阻止对 exec()的任何调用返回的运行时进程上的waitFor()

So any call to exec() should not block unless you used waitFor() on the returned process of the 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(); 
}

这篇关于如何告诉Java运行此Runtime.getRuntime()。exec,而不等待它必须运行的命令,只需在后端运行它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆