Difference between revisions of "User:Wvengen@nikhef.nl/BareBonesBrowserLaunch"

From PDP/Grid Wiki
Jump to navigationJump to search
m (bump minor revision of sourcecode as well)
(whoops approach doesn't work right now)
Line 1: Line 1:
 
Launching a web browser from a [http://java.sun.com/ Java] application has only been introduced recently. For older versions, [http://www.centerkey.com/java/browser/ 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 [https://jdic.dev.java.net/ JDIC] method, and falls back to the BareBonesBrowserLaunch method of calling the web browser manually when the former fails.
 
Launching a web browser from a [http://java.sun.com/ Java] application has only been introduced recently. For older versions, [http://www.centerkey.com/java/browser/ 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 [https://jdic.dev.java.net/ JDIC] method, and falls back to the BareBonesBrowserLaunch method of calling the web browser manually when the former fails.
 +
 +
'''This doesn't work at the moment, please stay tuned for an update'''
  
 
[http://www.nikhef.nl/pub/projects/grid/gridwiki/images/6/69/BareBonesBrowserLaunch.java download]
 
[http://www.nikhef.nl/pub/projects/grid/gridwiki/images/6/69/BareBonesBrowserLaunch.java download]

Revision as of 18:34, 3 April 2009

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.

This doesn't work at the moment, please stay tuned for an update

download

Please note that this hasn't received much testing yet, so please report your experiences.

   /////////////////////////////////////////////////////////
   //  Bare Bones Browser Launch                          //
   //  Version 1.5.99.1 (March 31, 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());
   	}
       }
   
   }