java – Desktop. Open() failed to silence on some jres
I'm trying to open a local HTML file on windows using the desktop class However, it only applies to some specific jres, not to others
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(); }
If it doesn't work, no exception is thrown and no text is printed in stderr
It applies to:
> JRE 1.6. 0_ 14 (32-bit) > JRE 1.7 0_ 05 (32-bit) > JRE 1.7 0_ 45 (64 bit) > JRE 1.7 0_ 51 (64 bit)
It does not apply to:
> JRE 1.6. 0_ 26 (64 bit) > JRE 1.6 0_ 37 (64 bit) > JRE 1.7 0_ 02 (64 bit) > JRE 1.7 0_ 21 (64 bit) edit: but applicable to CentOS
All tests are carried out on the same win7 64 bit box
Edit: the same problem occurred when trying to open a "TXT" or "PDF" file
thank you.
Solution
This problem has existed for some time, and some solutions have been recorded in at this blog
I have used the following code, which is reliable on every windows machine I use, regardless of JRE Sorry, I don't know all the jres I've tested
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + path);