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.
I don't think there's much you could do beyond:
- Check in common locations for common browsers (firefox, mozilla, etc.)
- Iterate the PATH environment variable looking for common browser executables.
- 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.)
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.
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.
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.)
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?
}
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