4

I'm trying to open a link in the default browser from Java. I do not want to create a simple web browser, I want to use an existing one. People have suggested to use the following:

Desktop.getDesktop().browse(new URI("http://www.targetsite.com"));

However, this simply hangs upon calling browse, and I end up having to force quit.

I saw a suggestion to run the following, to find out if Desktop and desktop.browse should be working on my system:

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

which gave the output:

Desktop IS supported on this platform 
Action BROWSE  IS supported on this platform 

What can I do to fix, or work around this?

Using Java 1.8.0_73

1 Answers1

6

It looks like you did everything correctly and your code should work as expected in Windows and OS X. Not surprisingly support for Desktop in Linux is not that great. You can try an alternative approach:

if (Runtime.getRuntime().exec(new String[] { "which", "xdg-open" }).getInputStream().read() != -1) {
    Runtime.getRuntime().exec(new String[] { "xdg-open", urlString });
}
martinez314
  • 12,162
  • 5
  • 36
  • 63