How do you invoke the default browser for Unix/OSX
Does anyone know how to programmatically display the contents of a URL in the default browser for a Unix system? I found a javaworld article on doing this using Runtime.getRuntime().exec(cmd). The article gives the cmd string for invoking the default browser in Windows, and for invoking the Netscape browser in Unix.
This can easily be extended to invoke any known browser executable in a known path on a Unix system; but I'd like to invoke the default browser without knowing what or where it is. I'm specifically targetting Mac/OSX, but any Unix solution is worth trying.
Is there a Unix command for launching the default browser to display the contents of a given URL? Or is there an alternative approach, rather than invoking Runtime.exec()?
-Mark
> By the way, why dont you try openening all possible
> browsers on that system? (If you cant find a way to
> open default browser)
>
> I think, depending on the error code you get, you can
> find whther opening is successfull or not.
I'm writing a desktop app that I'd like people to download for any platform with no external dependencies. So invoking tha native browser via JNI (per your earlier post) won't meet my objective.
Trying sequentially for all known browsers is a good idea. I had that in the back of my mind as a fallback. the method Runtime.exec() does return a status value that will enable me to detect success.
I was hoping there was some standard way to invoke the defaut browser on a Unix system generically, as I can with Windows. For example, the following code will dispatch the specified URL to the default browser in Windows:
cmd = "rundll32 url.dll,FileProtocolHandler http://some.url";
Runtime.getRuntime().exec();
If there's no equivalent capability in Unix, then I'll have to try
cmd = "some/path/some browser.exe"
Runtime.getRuntime().exec();
for each known browser platform and each likely path for locating applications. For Mac OSX, it makes sense to look in /Applications for the browser. I don't know if there is a standard path or sym link that points to application executables on Unix platforms. That's a bit off-topic for a java forum, but if someone could indulge this Unix newbie with an answer I'd appreciate it.
BTW, thanks very much for the tip on JDIC. I think it's a bit overkill for the particular app I'm discussing, and I'd have to bundle a platform-specific instance of the JDIC libraries for each targeted platform. But I think JDIC will be very useful for other projects where I'm willing to adopt that model.
-Mark
After extensive google searching, I came across a partial solution. This code detects the default browser on a Mac, and provides an elegant solution for trying standard browsers in turn for other Unix systems. I tested it on WIndows XP and Mac OSX 3.9, and it detected the default browser each time. A suped up version of this tool is available as a SourceForge project. You can find it here:
http://sourceforge.net/projects/browserlaunch2/
Here's the simplified code:
/////////////////////////////////////////////////////////
// Bare Bones Browser Launch //
// Version 1.5//
// December 10, 2005 //
// Supports: Mac OS X, GNU/Linux, Unix, Windows XP//
// Example Usage: //
//String url = "http://www.centerkey.com/";//
//BareBonesBrowserLaunch.openURL(url);//
// Public Domain Software -- Free to Use as You Like //
/////////////////////////////////////////////////////////
import java.lang.reflect.Method;
import javax.swing.JOptionPane;
public class BareBonesBrowserLaunch {
private static final String errMsg = "Error attempting to launch web browser";
public static void openURL(String url) {
String osName = System.getProperty("os.name");
try {
if (osName.startsWith("Mac OS")) {
Class fileMgr = Class.forName("com.apple.eio.FileManager");
Method openURL = fileMgr.getDeclaredMethod("openURL",
new Class[] {String.class});
openURL.invoke(null, new Object[] {url});
}
else if (osName.startsWith("Windows"))
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
else { //assume Unix or Linux
String[] browsers = {
"firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
String browser = null;
for (int count = 0; count < browsers.length && browser == null; count++)
if (Runtime.getRuntime().exec(
new String[] {"which", browsers[count]}).waitFor() == 0)
browser = browsers[count];
if (browser == null)
throw new Exception("Could not find web browser");
else
Runtime.getRuntime().exec(new String[] {browser, url});
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, errMsg + ":\n" + e.getLocalizedMessage());
}
}
}
Hi,
These are exactly the answers I was looking for thanks.
I've been looking at JDIC, but can;t figure out where to put the native binary files in Eclipse.
There is a dll for windows, some other binaries for linux, solaris etc.
I will launch the browser as an exteernal process.
Ultimately it would be nice to have the browser embedded in a pane within my app using JDIC.
If anyone has used JDIC within Eclipse on Ubuntu (or any linux), please post.