Monday, June 25, 2007

Troubles with java.awt.Desktop browse() method

New Desktop features were added in Java 6. One of those features is browse(URI uri) method, which launches the user-default browser to show a specified URI.
So, it is quite easy to show some web-page to user: check if desktop is supported, then check if BROWSE action is supported and call browse() method. Here is some code chunk:

try {
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(Desktop.Action.BROWSE)) {
desktop.browse(new URI("http://www.sun.com"));
}
}
} catch (Exception e) {
e.printStackTrace();
}

Everything seams to be fine. But when you run it on Fedora 6 on KDE it ends up with

java.io.IOException: Failed to show URI:http://www.sun.com
at sun.awt.X11.XDesktopPeer.launch(XDesktopPeer.java:75)
at sun.awt.X11.XDesktopPeer.browse(XDesktopPeer.java:64)
at java.awt.Desktop.browse(Desktop.java:368)
at Main.main(Main.java:12)

So, you go to Sun's bug database and start searching what is going on. And here it is: bug 6486393 (Need a KDE support in java.awt.Desktop class). So, it turns out that Desktop API was developed to support Windows and Gnome only. Upps.
OK, you are switching to Gnome on the same Fedora 6, run the example again and... you have the same exception. It does not work.
Two question are arising to Sun's engineers:
  1. Why KDE is simply ignored?
  2. Is it so difficult to add support even for Gnome that works?

4 comments:

Anonymous said...

Hallo,

I had the same problem to run this code line: Desktop.getDesktop().browse(new URI("www.sun.com")); in ubuntu 8.04 (gnome)!

The solution for my system is change in:
Desktop.getDesktop().browse(new URI("http://www.sun.com"));

or for a local file:
Desktop.getDesktop().browse(new URI("file:///home/.../index.html"));

sincerly yours
Danderson

Anonymous said...

Who knows it.

Anonymous said...

I have passed something?

Anonymous said...

I have problems when uploading pictures on facebook