【问题标题】:Desktop.getDesktop().browse HangsDesktop.getDesktop().browse 挂起
【发布时间】:2015-03-08 21:54:23
【问题描述】:

我正在开发一个应用程序,如果用户点击一个链接,我希望它在他们的默认浏览器中打开。根据我的阅读,这在理论上应该可以工作,但是,当在 Linux(特别是 Linux Mint 17.1)上运行时,它会挂起,直到程序被强制退出。我对在 WebView 中打开它不是特别感兴趣。大家能想到的任何替代方案或修复方法吗?提前致谢。

if(Desktop.isDesktopSupported()){
    try{
       Desktop.getDesktop().browse(new URI(url));
    }catch (IOException | URISyntaxException e){
       log.debug(e);
    }
}

【问题讨论】:

  • url 的典型值是多少?是http://..file://.. 还是别的什么?
  • http://是最常见的
  • Desktop.browse(..) 已知对于基于 file: 的 URI 会失败。请改用 Desktop.open(File)http: URI 是否也会失败?
  • 对文件没有试过:,对 http: 失败
  • 从 krzysiek.ste 试试这个,它对我有用:stackoverflow.com/a/28807079/8207181

标签: java awt native


【解决方案1】:

我使用的是 Ubuntu 16.04,并且在使用 Desktop.getDesktop().browse() 时遇到了同样的问题。这是我正在使用的解决方法:

public void browseURL(String urlString) {

    try {
        if (SystemUtils.IS_OS_LINUX) {
            // Workaround for Linux because "Desktop.getDesktop().browse()" doesn't work on some Linux implementations
            if (Runtime.getRuntime().exec(new String[] { "which", "xdg-open" }).getInputStream().read() != -1) {
                Runtime.getRuntime().exec(new String[] { "xdg-open", urlString });
            } else {
                showAlert("Browse URL", "xdg-open not supported!", true);
            }
        } else {
            if (Desktop.isDesktopSupported())
            {
                Desktop.getDesktop().browse(new URI(urlString));
            } else {
                showAlert("Browse URL", "Desktop command not supported!", true);
            }
        }

    } catch (IOException | URISyntaxException e) {
        showAlert("Browse URL", "Failed to open URL " + urlString , true);
    }
}

【讨论】:

    【解决方案2】:

    你从中得到了什么?:

    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 ");
    }
    

    另外,在 stackoverflow 上查看 thisthis 的答案。

    【讨论】:

      【解决方案3】:

      You are not alone。这是在 JDK 1.6 和 1.7 的某些版本中出现的错误。我还没有看到它出现在 JDK 1.8 中。

      它也可能发生在 Windows 上,您所能做的就是更新 JVM 或不使用 Desktop 类(这很糟糕)。

      【讨论】:

      • 壮丽的。好吧,写了一点解决方法。对我来说效果很好。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      相关资源
      最近更新 更多