6

I'm building a command line string in my Java application (e.g. "winword.exe FinalReport.doc"). I want to execute the command and let it go: I don't want/need to wait for the result (max would be: did it start right, but that's optional) and the application needs to continue running when my Java application has terminated.

I had a look at Runtime.getRuntime().exec() and Apache Commons_Exec. Both keep in control of the launched app (with commons and a callback clearly preferable), but that's not what I need. What do I miss?

Do I need to call the Windows/Linux/Mac API to launch an independent app?

miku
  • 181,842
  • 47
  • 306
  • 310
stwissel
  • 20,110
  • 6
  • 54
  • 101
  • Have you tried running "start winword.exe FinalReport.doc"? `start` starts winword and returns without waiting for it to exit. – Jacob Aug 10 '11 at 14:00

2 Answers2

8

try using Process! this is how I am using it in my app:

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

I would guess, you can use like:

Process process = new ProcessBuilder()
                    .command("winword.exe", "FinalReport.doc")
                    .directory(new File("C:/"))
                    .redirectErrorStream(true)
                    .start()
Arthur Neves
  • 11,840
  • 8
  • 60
  • 73
  • I'll try that. The really interesting question: What happens if the Java terminates before Windword is closed. – stwissel Aug 12 '11 at 14:55
  • as far as I know when u run processbuilder.start it forks to process ! so even if you close Java ! Windword still will keep run! – Arthur Neves Aug 12 '11 at 15:00
5

There is an easy cross-platform way to do this, using the java.awt.Desktop api, like this:

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

This opens up whatever application the user has specified has his preferred editor for that file, in another process entirely separate from your application (and you don't need to worry about possibly getting locked up from not reading from the created process' stdout the way you would using ProcessBuilder). You don't need to use platform-specific code or even know what applications are available on the user's platform.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • I had a look at that. Almost worked for me :-). My files sit on a remote webDAV server and on Windows the edit call first looks at the protocol (https://) and starts the browser instead of Winword. Would you know if Desktop could query the executable name? – stwissel Aug 12 '11 at 14:58
  • @stwissel: I think I'd probably write some code to copy the file at the url to the temp directory and open the file from there. But no, the Desktop api doesn't tell what it would use to open anything. – Nathan Hughes Aug 12 '11 at 15:11
  • thx for the explanation. Guess it would be interesting to either have a look at the OpenJDK sources to see what calls they make or do some serious reflection work. Can someone add a day to my week? – stwissel Aug 13 '11 at 03:46
  • @stwissel: that would entail a lot of digging. if you are interested in the windows-specific solution see http://stackoverflow.com/questions/6273221/open-a-text-file-in-the-default-text-editor-via-java/6273238#6273238 – Nathan Hughes Aug 13 '11 at 08:43
  • @stwissel: the trail drops off into native code at http://www.docjar.com/html/api/sun/awt/windows/WDesktopPeer.java.html – Nathan Hughes Aug 13 '11 at 08:53