Run a command from Java and DO NOT wait for the result

I am creating a command line in my Java application (for example, "winword.exe FinalReport.doc"). I want to execute a command and release it: I don’t want / should wait for the result (max would be: if it started correctly, but this is optional), and the application should continue to work when my Java application has stopped.

I looked Runtime.getRuntime().exec()and Apache Commons_Exec. Both control the running application (with community and callback privileges clearly preferable), but this is not what I need. What am I missing?

Do I need to use the Windows / Linux / Mac API to run an independent application?

+5
source share
2 answers

There is a simple cross-platform way to do this using java.awt.Desktopapi, for example:

File file = new File("FinalReport.doc");
java.awt.Desktop.getDesktop().edit(file);

This opens all the applications that the user has specified, has its own preferred editor for this file, in another process completely separate from your application (and you don’t need to worry about what is possibly blocked from reading from the created process) stdout, as you would use ProcessBuilder). You do not need to use code on the platform or even know which applications are available on the user platform.

+4
source

try using Process! this is how i use it in my application:

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

I would suggest you can use like:

Process process = new ProcessBuilder()
                    .command("winword.exe", "FinalReport.doc")
                    .directory(new File("C:/"))
                    .redirectErrorStream(true)
                    .start()
+7
source

All Articles