支持桌面和 desktop.browse,但浏览仍然挂起

Desktop and desktop.browse are supported, but browse still hangs

我正在尝试在 Java 的默认浏览器中打开 link。我不想创建一个简单的网络浏览器,我想使用现有的浏览器。人们建议使用以下内容:

Desktop.getDesktop().browse(new URI("http://www.targetsite.com"));

然而,这只是在调用 browse 时挂起,我最终不得不强制退出。

我看到了 运行 以下的建议,以查明 Desktop 和 desktop.browse 是否应该在我的系统上工作:

if (Desktop.isDesktopSupported()) {
    System.out.println("Desktop IS supported on this platform ");
    if (Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
        System.out.println("Action BROWSE  IS supported on this platform ");
    } else {
        System.out.println("Action BROWSE  ISN'T supported on this platform ");
    }
} else {
    System.out.println("Desktop ISN'T supported on this platform ");
}

给出了输出:

Desktop IS supported on this platform 
Action BROWSE  IS supported on this platform 

我能做些什么来修复或解决这个问题?

使用 Java 1.8.0_73

看起来你做的一切都正确,你的代码应该在 Windows 和 OS X 中按预期工作。不足为奇的是 Desktop 在 Linux 中的支持不是那很棒。您可以尝试另一种方法:

if (Runtime.getRuntime().exec(new String[] { "which", "xdg-open" }).getInputStream().read() != -1) {
    Runtime.getRuntime().exec(new String[] { "xdg-open", urlString });
}