A simple class to load an URL in a browser : Utilities « Network Protocol « Java






A simple class to load an URL in a browser

      
/* This code is part of Freenet. It is distributed under the GNU General
 * Public License, version 2 (or at your option any later version). See
 * http://www.gnu.org/ for further details of the GPL. */

//package freenet.utils;

import java.lang.reflect.Method;
import java.net.URL;

/**
 * A simple class to load an URL in a browser
 * 
 * @author Florent Daignière <nextgens@freenetproject.org>
 */
public class BrowserLaunch {
  private final static boolean isMacos;
  private final static boolean isWindows;
  private static String BROWSER = null;
  
  static {
    String osName = System.getProperty("os.name");
    isMacos = osName.startsWith("Mac OS");
    isWindows = osName.startsWith("Windows");
    if(!isMacos && !isWindows) {
      //assume Unix or Linux
      String[] browsers = {"firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape", "seamonkey"};

      try {
        for(int count = 0; count < browsers.length && BROWSER == null; count++)
          if(Runtime.getRuntime().exec(new String[]{"which", browsers[count]}).waitFor() == 0)
            BROWSER = browsers[count];
      } catch (Exception e) {}
    }
  }
  
        public static void main(String arg[]) {
    launch(arg[0]);
  }
  
  public static void launch(URL url) {
    launch(url.toString());
  }
  
  public static void launch(String url) {
                try {
                        if (isMacos) {
                                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 (isWindows)
        Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
                        else {
                                if (BROWSER == null)
                                        throw new Exception("Could not find web browser");
                                else 
                                        Runtime.getRuntime().exec(new String[] { BROWSER, url });
                        }
                }
                catch (Exception e) {
                        System.out.println("Unable to detect/startup your browser... please go to " + url + " for futher instructions");
                }
        }
}

   
    
    
    
    
    
  








Related examples in the same category

1.A class that encodes URL parameter values for MIDP devices.
2.Get the listing of everyone logged on
3.Scan your computer for ports in useScan your computer for ports in use
4.Using the URL Class (GetURL.java)
5.TCP socket monitor
6.Create Socket helper
7.Implements a TCP/IP bounce utility (proxy)
8.URL utilities class that makes it easy to create new URLs based off of old URLs without having to assemble or parse them yourself
9.Download from URL
10.URL ParserURL Parser
11.Using the java API URL class, extract the http/https hostname.
12.Utility class for URL decoding.
13.Utility class for URL encoding.
14.Allows easy downloading of files from HTTP sites
15.enum Http Status