User:Wvengen@nikhef.nl/BareBonesBrowserLaunch
From PDP/Grid Wiki
< User:Wvengen@nikhef.nl
Jump to navigationJump to searchRevision as of 09:41, 31 March 2009 by Wvengen@nikhef.nl (talk | contribs) (add xdg-open to browser programs and add some comments (in source code))
Launching a web browser from a Java application has only been introduced recently. For older versions, BareBonesBrowserLaunch has been around for a while. As a programmer, I don't want to have choose between them when the system can detect it for me. This version of BareBonesBrowserLaunch first tries the recent JDIC method, and falls back to the BareBonesBrowserLaunch method of calling the web browser manually when the former fails.
Please note that this hasn't received much testing yet, so please report your experiences.
///////////////////////////////////////////////////////// // Bare Bones Browser Launch // // Version 1.5.99.0 (March 30, 2009) // // By Dem Pilafian and Willem van Engen // // 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 java.net.URI; import java.net.URL; import javax.swing.JOptionPane; public class BareBonesBrowserLaunch { private static final String errMsg = "Error attempting to launch web browser"; //private static Logger logger = Logger.getLogger("com.centerkey.utils.BareBonesBrowserLaunch"); public static void openURL(URL url) { openURL(url.toExternalForm()); } public static void openURL(String url) { // Try java desktop API first (new in Java 1.6) try { Class desktop = Class.forName("java.awt.Desktop"); Method browse = desktop.getDeclaredMethod("browser", new Class[] {URI.class}); URI uri = new URI(url); //logger.fine("Using Java Desktop API to open URL '"+url+"'"); browse.invoke(null, new Object[] {uri}); return; } catch(Exception e) { } // Failed, resort to executing the browser manually 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}); //logger.fine("Using "+fileMgr+" to open URL '"+url+"'"); openURL.invoke(null, new Object[] {url}); } else if (osName.startsWith("Windows")) { String cmd = "rundll32 url.dll,FileProtocolHandler " + url; //logger.fine("Executing: "+cmd); Runtime.getRuntime().exec(cmd); } else { //assume Unix or Linux String[] browsers = { // Freedesktop, http://portland.freedesktop.org/xdg-utils-1.0/xdg-open.html "xdg-open", // Debian "sensible-browser", // Otherwise call browsers directly "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) { //logger.warning("No web browser found"); throw new Exception("Could not find web browser"); } else { //logger.fine("Executing: "+browser+" "+url); Runtime.getRuntime().exec(new String[] {browser, url}); } } } catch (Exception e) { JOptionPane.showMessageDialog(null, errMsg + ":\n" + e.getLocalizedMessage()); } } }