tags:

views:

593

answers:

6

I am trying to use Desktop.browse() to invoke a URL, this works fine on Windows machines or on Linux machines with a default browser configured. However, it throws an IOException exception when no default browser is found on Linux. What are some of the ways to work around this? I suppose I can attempt to launch Firefox and assume its there but I do not want to make that assumption.

+7  A: 

I don't think there's much you could do beyond:

  1. Check in common locations for common browsers (firefox, mozilla, etc.)
  2. Iterate the PATH environment variable looking for common browser executables.
  3. Ask the user in configuration.

Additionally, there is a whole section of the SWT FAQ dedicated to discovering the appropriate version of firefox to use on a particular system (keep reading the questions starting with the one linked above.)

Jared
+9  A: 

You can allow the user to enter the command they want to launch their browser, and then save that command so it will use that command everytime.

CookieOfFortune
using x-www-browser or www-browser as a default is a safe bet.
artificialidiot
+2  A: 

You can try various browsers in some order -- firefox, opera, etc, etc; also keep an editable configuration file which lets the user set a browser, remember there the one you found, etc.

Alex Martelli
Good idea!I would say that most users use only the popular browsers, and there aren't many of those.. So by making a list of 20 browsers, your chances of missing in such a test are slim. This test should be carried after you get this IOException.
Liran Orevi
+1  A: 

Try to execute xdg-open http://the/url first if you're going to implement one of the "cycle through a bunch of browsers". That should open the default browser if for some reason Java can't find it. (It does seem likely that this is what Java does anyway.)

Paul Fisher
Surprisingly, Java doesn't use xdg-open. See the source linked in my answer.
Matthew Flaschen
+3  A: 

try xdg-open or just try with konqueror (default on KDE, but not supported by Desktop API) and firefox.

Try also kmclient exec url.

if (Desktop.isDesktopSupported()) {
   desktop = Desktop.getDesktop();
   // blah blah
} else {
   // try to launch xdg-open
   // or try launching other browsers?
}
dfa
xdg-open uses kfmclient on KDE (which in turn will open konqueror if that's default), so there's no reason to open either konqueror or kfmclient directly.
Matthew Flaschen
I will try xdg-open for now, if it runs into further problems then I'll take the route of setting a configuration somewhere.
yx
+2  A: 

It looks like Desktop.browse() ultimately calls XDesktopPeer.browse() on *ix. That method is implemented by calling gnome_url_show. That probably works fine in some cases, but xdg-open is the cross-platform solution, as others have noted.

Arguably, this is a bug in Sun Java. Bug 6490730, "Desktop throws IOException instead of showing URL or sending mail", (reported November 2006) seems relevant

Matthew Flaschen