Desktop.getDesktop().browse Hangs
Asked Answered
R

3

5

I'm working on an application in which if a user clicks on a link, I want it to open in their default browser. From what I've read, this should in theory work, however, when run on Linux (specifically Linux Mint 17.1), it hangs until the program is force-quit. I'm not particularly interested in having it open in WebView. Any alternatives or fixes that you all can think of? Thanks in advance.

if(Desktop.isDesktopSupported()){
    try{
       Desktop.getDesktop().browse(new URI(url));
    }catch (IOException | URISyntaxException e){
       log.debug(e);
    }
}
Rexanna answered 10/1, 2015 at 18:42 Comment(6)
What is a typical value for url? Is it an http://.. or file://.. or something else?Offal
http:// is the most commonRexanna
Desktop.browse(..) is known to fail for file: based URIs. Use Desktop.open(File) instead. Does it also fail for http: URIs?Offal
Haven't tried it for file:, fails for http:Rexanna
Try this from krzysiek.ste, it worked for me: https://mcmap.net/q/158982/-how-to-open-url-in-default-webbrowser-using-javaBernadette
Does this answer your question? Desktop browse does not work in java for UbuntuGelatinoid
H
1

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).

Horsefly answered 10/1, 2015 at 20:33 Comment(1)
Magnificent. Well, wrote a bit of a work around. Works well enough for me.Rexanna
P
6

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);
    }
}
Pigeonhearted answered 12/3, 2019 at 13:39 Comment(0)
P
2

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.

Papery answered 10/1, 2015 at 21:7 Comment(0)
H
1

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).

Horsefly answered 10/1, 2015 at 20:33 Comment(1)
Magnificent. Well, wrote a bit of a work around. Works well enough for me.Rexanna

© 2022 - 2024 — McMap. All rights reserved.