Desktop.getDesktop().browse Hangs

此生再无相见时 提交于 2019-12-02 03:58:32

You are not alone. This is a bug that appears to happen in some versions of JDK 1.6 and 1.7. I haven't seen it occuring in JDK 1.8.

It can occur on Windows too and all you can do is either update the JVM or not use the Desktop class (which sucks).

I'm using Ubuntu 16.04 and I have the same hang when using Desktop.getDesktop().browse(). Here is the workaround that I'm using:

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

What do you get from this?:

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

Also, have a look at this and this answers here at stackoverflow.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!