xav xav - 2 months ago 12
Java Question

Desktop.open() silently fails on some JREs

I'm trying to use

class to open a local HTML file on Windows. But it works only for some specific JREs, and not with some other JREs.
Here is my code:

try {
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(Desktop.Action.OPEN)) {
desktop.open(new File("test.html"));
} else {
throw new Exception("OPEN action not supported");
}
} else {
throw new Exception("Desktop not supported");
}
} catch (Exception e) {
e.printStackTrace();
}


When it doesn't work, no Exception is thrown, and no text is printed in STDERR.

It works with:


  • JRE 1.6.0_14 (32 bits)

  • JRE 1.7.0_05 (32 bits)

  • JRE 1.7.0_45 (64 bits)

  • JRE 1.7.0_51 (64 bits)



It does not work with:


  • JRE 1.6.0_26 (64 bits)

  • JRE 1.6.0_37 (64 bits)

  • JRE 1.7.0_02 (64 bits)

  • JRE 1.7.0_21 (64 bits) EDIT : But works on CentOS



All tests were performed on the same Win7 64 bits box.

EDIT : Same issue when trying to open a "txt" or "pdf" file

Thank you.

xav xav
Answer

As "Stijn de Witt" said in the comments of "haventchecked"'s answer, the solution based on Runtime.getRuntime() doesn't work with UNC paths, and with paths containing consecutive spaces. Here is the solution that also works with these special paths:

new ProcessBuilder("rundll32", "url.dll,FileProtocolHandler", path).start();