Desktop.getDesktop().browse 挂起

erz*_*zr2 4 java native awt

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

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

Oli*_*ble 5

我使用的是 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);
    }
}
Run Code Online (Sandbox Code Playgroud)