从Java执行命令而不是等待结果

stw*_*sel 5 java registry java-native-interface command-line

我正在我的Java应用程序中构建一个命令行字符串(例如"winword.exe FinalReport.doc").我想执行命令然后放手:我不想/需要等待结果(最大值是:它是否正确启动,但这是可选的)当应用程序终止时应用程序需要继续运行.

我看了一下Runtime.getRuntime().exec()Apache Commons_Exec.两者都控制着已启动的应用程序(公共区域和回调显然更可取),但这不是我需要的.我错过了什么?

我是否需要调用Windows/Linux/Mac API来启动独立应用程序?

Art*_*ves 7

尝试使用Process!这是我在我的应用程序中使用它的方式:

Process process = new ProcessBuilder()
                    .command(convert, image.name, "-thumbnail", "800x600>", bigsize.name)
                    .directory(image.parentFile)
                    .redirectErrorStream(true)
                    .start()
Run Code Online (Sandbox Code Playgroud)

我猜,你可以使用:

Process process = new ProcessBuilder()
                    .command("winword.exe", "FinalReport.doc")
                    .directory(new File("C:/"))
                    .redirectErrorStream(true)
                    .start()
Run Code Online (Sandbox Code Playgroud)


Nat*_*hes 5

有一种使用java.awt.Desktopapi 的简单跨平台方法,例如:

File file = new File("FinalReport.doc");
java.awt.Desktop.getDesktop().edit(file);
Run Code Online (Sandbox Code Playgroud)

这将打开用户指定的任何应用程序,该应用程序具有该文件的首选编辑器,而这是在另一个与您的应用程序完全分开的进程中进行的(并且您不必担心由于不从已创建的进程的标准中读取而可能被锁定)您将使用ProcessBuilder)。您无需使用特定于平台的代码,甚至不需要知道用户平台上可用的应用程序。