10

I am writing a java application and I want to open a link from my program in user's default internet browser. I tried to use class Desktop like this :

if (Desktop.isDesktopSupported()) {
    Desktop desktop = Desktop.getDesktop();
    if (desktop.isSupported(java.awt.Desktop.Action.BROWSE)) {
        try {
            URI uri = new URI(url); // url is a string containing the URL
            desktop.browse(uri);
        }
        catch (URISyntaxException ex) {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

However first if returns false. My OS is newest version of Ubuntu. Does anybody know how to get that Desktop supported in java?

2 Answers 2

9

You don't need to run the java code on the Gnome desktop, per se. You just need to have the Gnome libraries installed so that Java recognizes it (as ccheneson said).

If you are running a new version of Ubuntu, it doesn't come with the gnome libraries because it uses Unity. Try installing libgnome2-0. When I installed it a few other packages came with it (libbonobo2-0, libbonobo2-common, libgnomevfs2-0, libgnomevfs2-common) so I don't know if libgnome2-0 is sufficient or if any of the others are necessary as well. But then my 12.04 Ubuntu system was recognized by the Java API.

I know this post is relatively old - but this question is in a variety of places online and the only place I found the "correct" answer (for me) was here

1
  • I think nowadays just having gtk3 or gtk2 installed should be enough (no need to pull old libgnome2). If libgnome is present it will use gnome_url_show() function and if GTK is present it will use gtk_show_uri().
    – Nelson
    Jan 17, 2021 at 4:52
6

From this article

Use the Desktop.isDesktopSupported() method to determine whether the Desktop API is available. On the Solaris Operating System and the Linux platform, this API is dependent on Gnome libraries. If those libraries are unavailable, this method will return false. After determining that the API is supported, that is, the isDesktopSupported() returns true, the application can retrieve a Desktop instance using the static method getDesktop().

Try running your code on Gnome desktop.

1

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.