Desktop.open()在某些JRE上静默失败 [英] Desktop.open() silently fails on some JREs

查看:146
本文介绍了Desktop.open()在某些JRE上静默失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 桌面 类在Windows上打开本地HTML文件。但它仅适用于某些特定的JRE,而不适用于其他一些JRE。
这是我的代码:

I'm trying to use Desktop 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();
}

当它不起作用时,不会抛出异常,也没有文字是以STDERR打印。

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

适用于:


  • JRE 1.6.0_14( 32位)

  • JRE 1.7.0_05(32位)

  • JRE 1.7.0_45(64位)

  • JRE 1.7.0_51(64位)

  • 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)

使用:


  • JRE 1.6.0_26(64位)

  • JRE 1.6.0_37(64位)

  • JRE 1.7.0_02(64位)

  • JRE 1.7.0_21(64位)编辑:但适用于CentOS

  • 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

所有测试都在同一个Win7 64位盒子上进行。

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

编辑:尝试打开txt或pdf文件时出现同样的问题

谢谢。

推荐答案

正如Stijn de Witthaventchecked的回答中所说的,解决方案基于 Runtime.getRuntime()的确如此不适用于UNC路径,并且路径包含连续的空格。以下是适用于这些特殊路径的解决方案:

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();

这篇关于Desktop.open()在某些JRE上静默失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆