java - 支持桌面和桌面浏览器,但浏览器仍然挂起

标签 java url web desktop

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

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 中按预期工作。毫不奇怪,Linux 中对 Desktop 的支持不是很好。您可以尝试另一种方法:

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

关于java - 支持桌面和桌面浏览器,但浏览器仍然挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37926495/

相关文章:

java - 一次 JSF/Seam 更改的多个 AJAX 请求

java - 递归使用 Stream.flatMap()

c++ - 在 MFC 中使用 WinINet 的问题

java - 使用 fieldgroup 属性 vaadin 进行验证

javascript - 我可以在不破坏新浏览器的情况下使用 "one fits all"polyfill 集合吗?

java - 接口(interface)寻找诸如 Appendable 或 OutputStream 之类的东西

java - Java 中的 Facebook Canvas 页面 - 可选的应用程序授权?

php - 对可能不包含协议(protocol)的字符串运行 parse_url()

objective-c - 沙箱: How to bookmark enclosing folder of user-selected file?

javascript - Watin - 填写表格后提交按钮保持禁用状态