1

The Desktop.getDestop().open(File) launches the associated aplication to open the file.

The Desktop class is available since Java 1.6 - http://docs.oracle.com/javase/6/docs/api/java/awt/Desktop.html

How to do the same using the 1.4 Java version?

2 Answers2

1

you can use the following to open files with the default application:

    /* build up command and launch */
    String command = "";
    String file = "FILE IN HERE";
    if (isLinux()) {
        command = "xdg-open " + file;
    } else if (isWindows()) {
        command = "cmd /C start " + file;
    } else
        return;

    try {
        Runtime.getRuntime().exec(command);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

available since 1.0: Runtime.

Baz
  • 36,440
  • 11
  • 68
  • 94
  • You can use [Apache commons-exec's `OS` class](http://commons.apache.org/proper/commons-exec/apidocs/org/apache/commons/exec/OS.html) to determine the host operating system at runtime. – Zoltán Nov 25 '13 at 16:12
  • On Windows, if I start an executable using `cmd /C start` as you suggested, I don't get any information on whether it successfully started. Do you know of any workaround for this? – Zoltán Nov 26 '13 at 08:39
  • @Zoltán You can read the output using one of the answers [here](http://stackoverflow.com/questions/5711084/java-runtime-getruntime-getting-output-from-executing-a-command-line-program). – Baz Nov 27 '13 at 11:04
  • @Zoltán Or use [this](http://stackoverflow.com/questions/14616733/runtime-exec-lp-command-print-success). – Baz Nov 27 '13 at 14:37
  • What I meant is - if I use `cmd /C start` command, I lose the handle to the process because it starts a command window which in turn start my target process and exits. – Zoltán Nov 29 '13 at 09:06
  • At my machine the `xdg-open` command is not exectued when I call it like this from my `java`, but when I try to execute the exact same command in a terminal window, it works. Where is the problem? – zimmerrol Nov 12 '16 at 23:32
0

Runtime.exec()

More details can be found at: http://docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html

Afshin Moazami
  • 2,092
  • 5
  • 33
  • 55
davo
  • 380
  • 2
  • 11