Java Desktop.getDesktop().browse挂起

Java Desktop.getDesktop().browse挂起,java,awt,native,Java,Awt,Native,我正在开发一个应用程序,如果用户单击某个链接,我希望它在默认浏览器中打开。从我所读到的,这在理论上应该是可行的,但是,当在Linux(特别是LinuxMint17.1)上运行时,它会一直挂起,直到程序被强制退出。我对在WebView中打开它不是特别感兴趣。你们都能想到的任何替代方案或修复方案?提前谢谢 if(Desktop.isDesktopSupported()){ try{ Desktop.getDesktop().browse(new URI(url)); }

我正在开发一个应用程序,如果用户单击某个链接,我希望它在默认浏览器中打开。从我所读到的,这在理论上应该是可行的,但是,当在Linux(特别是LinuxMint17.1)上运行时,它会一直挂起,直到程序被强制退出。我对在WebView中打开它不是特别感兴趣。你们都能想到的任何替代方案或修复方案?提前谢谢

if(Desktop.isDesktopSupported()){
    try{
       Desktop.getDesktop().browse(new URI(url));
    }catch (IOException | URISyntaxException e){
       log.debug(e);
    }
}
。这是JDK1.6和1.7的某些版本中出现的一个bug。我还没有在JDK1.8中看到这种情况


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

您从中得到了什么

if(Desktop.isDesktopSupported()){
System.out.println(“此平台支持桌面”);
if(Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)){
System.out.println(“此平台支持操作浏览”);
}
否则{
System.out.println(“此平台不支持操作浏览”);
}
}
否则{
System.out.println(“此平台不支持桌面”);
}

另外,请看一下stackoverflow,并在这里找到答案。

我使用的是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);
    }
}

url
的典型值是什么?这是一个
http://..
file://..
或其他什么?http://是最常见的
桌面。众所周知,基于
文件的URI浏览(…)
失败。改用
桌面。打开(文件)
。对于
http:
uri它也失败了吗?还没有对file:,对http尝试过:从krzysiek.ste尝试这个,它对我有效:很棒。嗯,我写了一篇文章。对我来说已经足够好了。