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

From PDP/Grid Wiki
Jump to navigationJump to search
(new version)
m (Remove final bracket :o)
 
(One intermediate revision by the same user not shown)
Line 14: Line 14:
  
 
     /////////////////////////////////////////////////////////
 
     /////////////////////////////////////////////////////////
 
 
     //  Bare Bones Browser Launch                          //
 
     //  Bare Bones Browser Launch                          //
 
 
     //  Version 1.5.99.3 (April 6, 2009)                  //
 
     //  Version 1.5.99.3 (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    //
 
 
     //  Example Usage:                                    //
 
     //  Example Usage:                                    //
 
 
     //    String url = "http://www.centerkey.com/";      //
 
     //    String url = "http://www.centerkey.com/";      //
 
 
     //    BareBonesBrowserLaunch.openURL(url);            //
 
     //    BareBonesBrowserLaunch.openURL(url);            //
 
 
     //  Public Domain Software -- Free to Use as You Like  //
 
     //  Public Domain Software -- Free to Use as You Like  //
 
 
     /////////////////////////////////////////////////////////
 
     /////////////////////////////////////////////////////////
 
 
      
 
      
 
 
     import java.awt.Component;
 
     import java.awt.Component;
 
 
     import java.lang.reflect.Method;
 
     import java.lang.reflect.Method;
 
 
     import java.net.URI;
 
     import java.net.URI;
 
 
     import java.net.URL;
 
     import java.net.URL;
 
 
     import javax.swing.JOptionPane;
 
     import javax.swing.JOptionPane;
 
 
      
 
      
 
 
     /** Class to open the system's default web browser.
 
     /** Class to open the system's default web browser.
 
+
     * <p>
     * <p>
 
 
 
 
     * Until Java 1.6, there was no standard way to open an internet page with the
 
     * Until Java 1.6, there was no standard way to open an internet page with the
 
 
     * user's default web browser. This class aims to be a simple just-works solution
 
     * user's default web browser. This class aims to be a simple just-works solution
 
 
     * to this. It tries to use Java 1.6's method first, and falls back to running
 
     * to this. It tries to use Java 1.6's method first, and falls back to running
 
 
     * an executable (which requires full system permissions, of course), which is
 
     * an executable (which requires full system permissions, of course), which is
 
 
     * different for each platform.
 
     * different for each platform.
 
+
     * &lt;p&gt;
     * <p>
 
 
 
 
     * This code is an adaptation of
 
     * This code is an adaptation of
     * <a href="http://www.centerkey.com/java/browser/">http://www.centerkey.com/java/browser/</a>
+
     * &lt;a href="http://www.centerkey.com/java/browser/"&gt;http://www.centerkey.com/java/browser/&lt;/a&gt;
 
 
 
     */
 
     */
 
 
     public class BareBonesBrowserLaunch {
 
     public class BareBonesBrowserLaunch {
 
+
 
   
 
 
 
 
         private static final String errMsg = "Error attempting to launch web browser";
 
         private static final String errMsg = "Error attempting to launch web browser";
 
+
 
   
 
 
 
 
         /** Open a URL
 
         /** Open a URL
 
 
         *  
 
         *  
 
 
         * @param url {@link URL} to open
 
         * @param url {@link URL} to open
 
 
         * @param parent parent component for error message dialog
 
         * @param parent parent component for error message dialog
 
 
         */
 
         */
 
 
         public static void openURL(URL url, Component parent) {
 
         public static void openURL(URL url, Component parent) {
 
 
         openURL(url.toExternalForm(), parent);
 
         openURL(url.toExternalForm(), parent);
 
 
         }
 
         }
 
 
      
 
      
 
 
         /** Open a string URL
 
         /** Open a string URL
 
 
         *  
 
         *  
 
 
         * @param surl URL to open (as String)
 
         * @param surl URL to open (as String)
 
 
         * @param parent parent component for error message dialog
 
         * @param parent parent component for error message dialog
 
 
         */
 
         */
 
 
         @SuppressWarnings("unchecked") // to support older java compilers
 
         @SuppressWarnings("unchecked") // to support older java compilers
 
 
         public static void openURL(String surl, Component parent) {
 
         public static void openURL(String surl, Component parent) {
 
 
     // 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));
 
     // 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 getDesktop = desktop.getDeclaredMethod("getDesktop", new Class[] {});
 
         Method getDesktop = desktop.getDeclaredMethod("getDesktop", new Class[] {});
 
 
         Object desktopInstance = getDesktop.invoke(null, new Object[] {});
 
         Object desktopInstance = getDesktop.invoke(null, new Object[] {});
 
 
         Method browse = desktop.getDeclaredMethod("browse", new Class[] {URI.class});
 
         Method browse = desktop.getDeclaredMethod("browse", new Class[] {URI.class});
 
 
         URI uri = new URI(surl);
 
         URI uri = new URI(surl);
 
 
         //logger.fine("Using Java Desktop API to open URL '"+url+"'");
 
         //logger.fine("Using Java Desktop API to open URL '"+url+"'");
 
 
         browse.invoke(desktopInstance, new Object[] {uri});
 
         browse.invoke(desktopInstance, new Object[] {uri});
 
 
         return;
 
         return;
 
 
     } catch(Exception e) { }
 
     } catch(Exception e) { }
 
 
    
 
    
 
 
     // Failed, resort to executing the browser manually
 
     // Failed, resort to executing the browser manually
 
 
     String osName = System.getProperty("os.name");
 
     String osName = System.getProperty("os.name");
 
 
     try {
 
     try {
 
 
         // Mac OS has special Java class
 
         // Mac OS has special Java class
 
 
         if (osName.startsWith("Mac OS")) {
 
         if (osName.startsWith("Mac OS")) {
 
 
     Class fileMgr = Class.forName("com.apple.eio.FileManager");
 
     Class fileMgr = Class.forName("com.apple.eio.FileManager");
 
 
     Method openURL = fileMgr.getDeclaredMethod("openURL",
 
     Method openURL = fileMgr.getDeclaredMethod("openURL",
 
 
     new Class[] {String.class});
 
     new Class[] {String.class});
 
 
     //logger.fine("Using "+fileMgr+" to open URL '"+url+"'");
 
     //logger.fine("Using "+fileMgr+" to open URL '"+url+"'");
 
 
     openURL.invoke(null, new Object[] {surl});
 
     openURL.invoke(null, new Object[] {surl});
 
 
     return;
 
     return;
 
 
         }
 
         }
 
 
          
 
          
 
 
         String[] cmd = null;
 
         String[] cmd = null;
 
 
          
 
          
 
 
         // Windows execs url.dll
 
         // Windows execs url.dll
 
 
         if (osName.startsWith("Windows")) {
 
         if (osName.startsWith("Windows")) {
 
 
     cmd = new String[] { "rundll32", "url.dll,FileProtocolHandler", surl };
 
     cmd = new String[] { "rundll32", "url.dll,FileProtocolHandler", surl };
 
 
    
 
    
 
 
         // else assume unix/linux: call one of the available browsers
 
         // else assume unix/linux: call one of the available browsers
 
 
         } else {
 
         } else {
 
 
     String[] browsers = {
 
     String[] browsers = {
 
 
     // Freedesktop, http://portland.freedesktop.org/xdg-utils-1.0/xdg-open.html
 
     // Freedesktop, http://portland.freedesktop.org/xdg-utils-1.0/xdg-open.html
 
 
     "xdg-open",
 
     "xdg-open",
 
 
     // Debian
 
     // Debian
 
 
     "sensible-browser",
 
     "sensible-browser",
 
 
     // Otherwise call browsers directly
 
     // Otherwise call browsers directly
 
 
     "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
 
     "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
 
 
     String browser = null;
 
     String browser = null;
 
 
     for (int count = 0; count < browsers.length && browser == null; count++)
 
     for (int count = 0; count < browsers.length && browser == null; count++)
 
 
         if (Runtime.getRuntime().exec(
 
         if (Runtime.getRuntime().exec(
 
 
         new String[] {"which", browsers[count]}).waitFor() == 0)
 
         new String[] {"which", browsers[count]}).waitFor() == 0)
 
 
     browser = browsers[count];
 
     browser = browsers[count];
 
 
    
 
    
 
 
     if (browser == null) {
 
     if (browser == null) {
 
 
         //logger.warning("No web browser found");
 
         //logger.warning("No web browser found");
 
 
         throw new Exception("Could not find web browser");
 
         throw new Exception("Could not find web browser");
 
 
     }
 
     }
 
 
    
 
    
 
 
     cmd = new String[] { browser, surl };
 
     cmd = new String[] { browser, surl };
 
 
         }
 
         }
 
 
    
 
    
 
+
         if (Runtime.getRuntime().exec(cmd).waitFor() !=0 )
         if (Runtime.getRuntime().exec(cmd).waitFor()!=0)
+
  throw new Exception("Error opening page: "+surl);
    throw new Exception("Error opening page: "+surl);
 
 
 
 
     }
 
     }
 
 
     catch (Exception e) {
 
     catch (Exception e) {
 
 
         JOptionPane.showMessageDialog(parent, errMsg + ":\n" + e.getLocalizedMessage());
 
         JOptionPane.showMessageDialog(parent, errMsg + ":\n" + e.getLocalizedMessage());
 
 
     }
 
     }
 
 
         }
 
         }
 
 
          
 
          
 
 
         /** Open a URL
 
         /** Open a URL
 
+
         * &lt;p&gt;
         * <p>
 
 
 
 
         * This is equal to {@link #openURL(URL, Component) openURL(url, null)}
 
         * This is equal to {@link #openURL(URL, Component) openURL(url, null)}
 
         * so any error dialog will have no parent.
 
         * so any error dialog will have no parent.
 
 
         *  
 
         *  
 
 
         * @param url {@link URL} to open
 
         * @param url {@link URL} to open
 
 
         */
 
         */
 
 
         public static void openURL(URL url) {
 
         public static void openURL(URL url) {
 
 
         openURL(url, null);
 
         openURL(url, null);
 
 
         }
 
         }
 
 
      
 
      
 
 
         /** Open a string URL
 
         /** Open a string URL
 
+
         * &lt;p&gt;
         * <p>
 
 
 
 
         * This is equal to {@link #openURL(String, Component) openURL(surl, null)}
 
         * This is equal to {@link #openURL(String, Component) openURL(surl, null)}
 
         * so any error dialog will have no parent.
 
         * so any error dialog will have no parent.
 
 
         *  
 
         *  
 
 
         * @param surl URL to open (as String)
 
         * @param surl URL to open (as String)
 
 
         */
 
         */
 
 
         public static void openURL(String surl) {
 
         public static void openURL(String surl) {
 
 
         openURL(surl, null);
 
         openURL(surl, null);
 
 
         }
 
         }
 
 
     }
 
     }

Latest revision as of 10:27, 15 July 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

This code compiles on java 1.5 and 1.6 (and probably lower versions as well). The nice thing is that you can compile using an older compiler and still use the newer JDesktop method when running on an new JVM. A little testing to show this:

  • Compiler 1.6 target 1.6, JVM 1.6: JDesktop method
  • Compiler 1.6 target 1.5, JVM 1.6: JDesktop method
  • Compiler 1.6 target 1.6, JVM 1.5: doesn't run: "Bad version number in .class file" (of course)
  • Compiler 1.6 target 1.5, JVM 1.5: Exec method
  • Compiler 1.5 target 1.5, JVM 1.6: JDesktop method
  • Compiler 1.5 target 1.5, JVM 1.5: Exec method

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

   /////////////////////////////////////////////////////////
   //  Bare Bones Browser Launch                          //
   //  Version 1.5.99.3 (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.awt.Component;
   import java.lang.reflect.Method;
   import java.net.URI;
   import java.net.URL;
   import javax.swing.JOptionPane;
   
   /** Class to open the system's default web browser.
    * <p>
    * Until Java 1.6, there was no standard way to open an internet page with the
    * user's default web browser. This class aims to be a simple just-works solution
    * to this. It tries to use Java 1.6's method first, and falls back to running
    * an executable (which requires full system permissions, of course), which is
    * different for each platform.
    * <p>
    * This code is an adaptation of
    * <a href="http://www.centerkey.com/java/browser/">http://www.centerkey.com/java/browser/</a>
    */
   public class BareBonesBrowserLaunch {
 
       private static final String errMsg = "Error attempting to launch web browser";
  
       /** Open a URL
        * 
        * @param url {@link URL} to open
        * @param parent parent component for error message dialog
        */
       public static void openURL(URL url, Component parent) {
   	    openURL(url.toExternalForm(), parent);
       }
   
       /** Open a string URL
        * 
        * @param surl URL to open (as String)
        * @param parent parent component for error message dialog
        */
       @SuppressWarnings("unchecked") // to support older java compilers
       public static void openURL(String surl, Component parent) {
   	// 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(surl);
   	    //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 {
   	    // Mac OS has special Java class
   	    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[] {surl});
   		return;
   	    }
   	    
   	    String[] cmd = null;
   	    
   	    // Windows execs url.dll
   	    if (osName.startsWith("Windows")) {
   		cmd = new String[] { "rundll32", "url.dll,FileProtocolHandler", surl };
   		
   	    // else assume unix/linux: call one of the available browsers
   	    } else {
   		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");
   		}
   		
   		cmd = new String[] { browser, surl };
   	    }
   	
   	    if (Runtime.getRuntime().exec(cmd).waitFor() !=0 )
  		throw new Exception("Error opening page: "+surl);
   	}
   	catch (Exception e) {
   	    JOptionPane.showMessageDialog(parent, errMsg + ":\n" + e.getLocalizedMessage());
   	}
       }
       
       /** Open a URL
        * <p>
        * This is equal to {@link #openURL(URL, Component) openURL(url, null)}
        * so any error dialog will have no parent.
        * 
        * @param url {@link URL} to open
        */
       public static void openURL(URL url) {
   	    openURL(url, null);
       }
   
       /** Open a string URL
        * <p>
        * This is equal to {@link #openURL(String, Component) openURL(surl, null)}
        * so any error dialog will have no parent.
        * 
        * @param surl URL to open (as String)
        */
       public static void openURL(String surl) {
   	    openURL(surl, null);
       }
   }