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

From PDP/Grid Wiki
Jump to navigationJump to search
(whoops approach doesn't work right now)
(make Java1.6 desktop api call work)
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]
Line 9: Line 7:
 
     /////////////////////////////////////////////////////////
 
     /////////////////////////////////////////////////////////
 
     //  Bare Bones Browser Launch                          //
 
     //  Bare Bones Browser Launch                          //
     //  Version 1.5.99.1 (March 31, 2009)                 //
+
     //  Version 1.5.99.2 (April 6, 2009)                   //
 
     //  By Dem Pilafian and Willem van Engen              //
 
     //  By Dem Pilafian and Willem van Engen              //
 
     //  Supports: Mac OS X, GNU/Linux, Unix, Windows XP    //
 
     //  Supports: Mac OS X, GNU/Linux, Unix, Windows XP    //
Line 35: Line 33:
 
         public static void openURL(String url) {
 
         public static void openURL(String url) {
 
     // Try java desktop API first (new in Java 1.6)
 
     // Try java desktop API first (new in Java 1.6)
 +
    // basically: java.awt.Desktop.getDesktop().browse(new URI(url));
 
     try {
 
     try {
 
         Class desktop = Class.forName("java.awt.Desktop");
 
         Class desktop = Class.forName("java.awt.Desktop");
         Method browse = desktop.getDeclaredMethod("browser",
+
        Method getDesktop = desktop.getDeclaredMethod("getDesktop", new Class[] {});
        new Class[] {URI.class});
+
        Object desktopInstance = getDesktop.invoke(null, new Object[] {});
 +
         Method browse = desktop.getDeclaredMethod("browse", new Class[] {URI.class});
 
         URI uri = new URI(url);
 
         URI uri = new URI(url);
         //logger.fine("Using Java Desktop API to open URL '"+url+"'");
+
         logger.fine("Using Java Desktop API to open URL '"+url+"'");
         browse.invoke(null, new Object[] {uri});
+
         browse.invoke(desktopInstance, new Object[] {uri});
 
         return;
 
         return;
 
     } catch(Exception e) { }
 
     } catch(Exception e) { }
Line 80: Line 80:
 
     }
 
     }
 
         }
 
         }
     }
+
     } catch (Exception e) {
    catch (Exception e) {
 
 
         JOptionPane.showMessageDialog(null, errMsg + ":\n" + e.getLocalizedMessage());
 
         JOptionPane.showMessageDialog(null, errMsg + ":\n" + e.getLocalizedMessage());
 
     }
 
     }

Revision as of 07:30, 6 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.

download

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

   /////////////////////////////////////////////////////////
   //  Bare Bones Browser Launch                          //
   //  Version 1.5.99.2 (April 6, 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)
   	// basically: java.awt.Desktop.getDesktop().browse(new URI(url));
   	try {
   	    Class desktop = Class.forName("java.awt.Desktop");
   	    Method getDesktop = desktop.getDeclaredMethod("getDesktop", new Class[] {});
   	    Object desktopInstance = getDesktop.invoke(null, new Object[] {});
   	    Method browse = desktop.getDeclaredMethod("browse", new Class[] {URI.class});
   	    URI uri = new URI(url);
   	    logger.fine("Using Java Desktop API to open URL '"+url+"'");
   	    browse.invoke(desktopInstance, 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());
   	}
       }
   
   }