1 package edu.stanford.ejalbert;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6 import java.lang.reflect.Constructor;
7 import java.lang.reflect.Field;
8 import java.lang.reflect.InvocationTargetException;
9 import java.lang.reflect.Method;
12 * BrowserLauncher is a class that provides one static method, openURL, which opens the default
13 * web browser for the current user of the system to the given URL. It may support other
14 * protocols depending on the system -- mailto, ftp, etc. -- but that has not been rigorously
15 * tested and is not guaranteed to work.
17 * Yes, this is platform-specific code, and yes, it may rely on classes on certain platforms
18 * that are not part of the standard JDK. What we're trying to do, though, is to take something
19 * that's frequently desirable but inherently platform-specific -- opening a default browser --
20 * and allow programmers (you, for example) to do so without worrying about dropping into native
21 * code or doing anything else similarly evil.
23 * Anyway, this code is completely in Java and will run on all JDK 1.1-compliant systems without
24 * modification or a need for additional libraries. All classes that are required on certain
25 * platforms to allow this to run are dynamically loaded at runtime via reflection and, if not
26 * found, will not cause this to do anything other than returning an error when opening the
29 * There are certain system requirements for this class, as it's running through Runtime.exec(),
30 * which is Java's way of making a native system call. Currently, this requires that a Macintosh
31 * have a Finder which supports the GURL event, which is true for Mac OS 8.0 and 8.1 systems that
32 * have the Internet Scripting AppleScript dictionary installed in the Scripting Additions folder
33 * in the Extensions folder (which is installed by default as far as I know under Mac OS 8.0 and
34 * 8.1), and for all Mac OS 8.5 and later systems. On Windows, it only runs under Win32 systems
35 * (Windows 95, 98, and NT 4.0, as well as later versions of all). On other systems, this drops
36 * back from the inherently platform-sensitive concept of a default browser and simply attempts
37 * to launch Netscape via a shell command.
39 * This code is Copyright 1999-2001 by Eric Albert (ejalbert@cs.stanford.edu) and may be
40 * redistributed or modified in any form without restrictions as long as the portion of this
41 * comment from this paragraph through the end of the comment is not removed. The author
42 * requests that he be notified of any application, applet, or other binary that makes use of
43 * this code, but that's more out of curiosity than anything and is not required. This software
44 * includes no warranty. The author is not repsonsible for any loss of data or functionality
45 * or any adverse or unexpected effects of using this software.
48 * <br>Steven Spencer, JavaWorld magazine (<a href="http://www.javaworld.com/javaworld/javatips/jw-javatip66.html">Java Tip 66</a>)
49 * <br>Thanks also to Ron B. Yeh, Eric Shapiro, Ben Engber, Paul Teitlebaum, Andrea Cantatore,
50 * Larry Barowski, Trevor Bedzek, Frank Miedrich, and Ron Rabakukk
52 * @author Eric Albert (<a href="mailto:ejalbert@cs.stanford.edu">ejalbert@cs.stanford.edu</a>)
53 * @version 1.4b1 (Released June 20, 2001)
55 public class BrowserLauncher {
58 * The Java virtual machine that we are running on. Actually, in most cases we only care
59 * about the operating system, but some operating systems require us to switch on the VM. */
60 private static int jvm;
62 /** The browser for the system */
63 private static Object browser;
66 * Caches whether any classes, methods, and fields that are not part of the JDK and need to
67 * be dynamically loaded at runtime loaded successfully.
69 * Note that if this is <code>false</code>, <code>openURL()</code> will always return an
72 private static boolean loadedWithoutErrors;
74 /** The com.apple.mrj.MRJFileUtils class */
75 private static Class mrjFileUtilsClass;
77 /** The com.apple.mrj.MRJOSType class */
78 private static Class mrjOSTypeClass;
80 /** The com.apple.MacOS.AEDesc class */
81 private static Class aeDescClass;
83 /** The <init>(int) method of com.apple.MacOS.AETarget */
84 private static Constructor aeTargetConstructor;
86 /** The <init>(int, int, int) method of com.apple.MacOS.AppleEvent */
87 private static Constructor appleEventConstructor;
89 /** The <init>(String) method of com.apple.MacOS.AEDesc */
90 private static Constructor aeDescConstructor;
92 /** The findFolder method of com.apple.mrj.MRJFileUtils */
93 private static Method findFolder;
95 /** The getFileCreator method of com.apple.mrj.MRJFileUtils */
96 private static Method getFileCreator;
98 /** The getFileType method of com.apple.mrj.MRJFileUtils */
99 private static Method getFileType;
101 /** The openURL method of com.apple.mrj.MRJFileUtils */
102 private static Method openURL;
104 /** The makeOSType method of com.apple.MacOS.OSUtils */
105 private static Method makeOSType;
107 /** The putParameter method of com.apple.MacOS.AppleEvent */
108 private static Method putParameter;
110 /** The sendNoReply method of com.apple.MacOS.AppleEvent */
111 private static Method sendNoReply;
113 /** Actually an MRJOSType pointing to the System Folder on a Macintosh */
114 private static Object kSystemFolderType;
116 /** The keyDirectObject AppleEvent parameter type */
117 private static Integer keyDirectObject;
119 /** The kAutoGenerateReturnID AppleEvent code */
120 private static Integer kAutoGenerateReturnID;
122 /** The kAnyTransactionID AppleEvent code */
123 private static Integer kAnyTransactionID;
125 /** The linkage object required for JDirect 3 on Mac OS X. */
126 private static Object linkage;
128 /** The framework to reference on Mac OS X */
129 private static final String JDirect_MacOSX = "/System/Library/Frameworks/Carbon.framework/Frameworks/HIToolbox.framework/HIToolbox";
131 /** JVM constant for MRJ 2.0 */
132 private static final int MRJ_2_0 = 0;
134 /** JVM constant for MRJ 2.1 or later */
135 private static final int MRJ_2_1 = 1;
137 /** JVM constant for Java on Mac OS X 10.0 (MRJ 3.0) */
138 private static final int MRJ_3_0 = 3;
140 /** JVM constant for MRJ 3.1 */
141 private static final int MRJ_3_1 = 4;
143 /** JVM constant for any Windows NT JVM */
144 private static final int WINDOWS_NT = 5;
146 /** JVM constant for any Windows 9x JVM */
147 private static final int WINDOWS_9x = 6;
149 /** JVM constant for any other platform */
150 private static final int OTHER = -1;
153 * The file type of the Finder on a Macintosh. Hardcoding "Finder" would keep non-U.S. English
154 * systems from working properly.
156 private static final String FINDER_TYPE = "FNDR";
159 * The creator code of the Finder on a Macintosh, which is needed to send AppleEvents to the
162 private static final String FINDER_CREATOR = "MACS";
164 /** The name for the AppleEvent type corresponding to a GetURL event. */
165 private static final String GURL_EVENT = "GURL";
168 * The first parameter that needs to be passed into Runtime.exec() to open the default web
169 * browser on Windows.
171 private static final String FIRST_WINDOWS_PARAMETER = "/c";
173 /** The second parameter for Runtime.exec() on Windows. */
174 private static final String SECOND_WINDOWS_PARAMETER = "start";
177 * The third parameter for Runtime.exec() on Windows. This is a "title"
178 * parameter that the command line expects. Setting this parameter allows
179 * URLs containing spaces to work.
181 private static final String THIRD_WINDOWS_PARAMETER = "\"\"";
184 * The shell parameters for Netscape that opens a given URL in an already-open copy of Netscape
185 * on many command-line systems.
187 private static final String NETSCAPE_REMOTE_PARAMETER = "-remote";
188 private static final String NETSCAPE_OPEN_PARAMETER_START = "'openURL(";
189 private static final String NETSCAPE_OPEN_PARAMETER_END = ")'";
192 * The message from any exception thrown throughout the initialization process.
194 private static String errorMessage;
197 * An initialization block that determines the operating system and loads the necessary
201 loadedWithoutErrors = true;
202 String osName = System.getProperty("os.name");
203 if (osName.startsWith("Mac OS")) {
204 String mrjVersion = System.getProperty("mrj.version");
205 String majorMRJVersion = mrjVersion.substring(0, 3);
207 double version = Double.valueOf(majorMRJVersion).doubleValue();
210 } else if (version >= 2.1 && version < 3) {
211 // Assume that all 2.x versions of MRJ work the same. MRJ 2.1 actually
212 // works via Runtime.exec() and 2.2 supports that but has an openURL() method
213 // as well that we currently ignore.
215 } else if (version == 3.0) {
217 } else if (version >= 3.1) {
218 // Assume that all 3.1 and later versions of MRJ work the same.
221 loadedWithoutErrors = false;
222 errorMessage = "Unsupported MRJ version: " + version;
224 } catch (NumberFormatException nfe) {
225 loadedWithoutErrors = false;
226 errorMessage = "Invalid MRJ version: " + mrjVersion;
228 } else if (osName.startsWith("Windows")) {
229 if (osName.indexOf("9") != -1) {
238 if (loadedWithoutErrors) { // if we haven't hit any errors yet
239 loadedWithoutErrors = loadClasses();
244 * This class should be never be instantiated; this just ensures so.
246 private BrowserLauncher() { }
249 * Called by a static initializer to load any classes, fields, and methods required at runtime
250 * to locate the user's web browser.
251 * @return <code>true</code> if all intialization succeeded
252 * <code>false</code> if any portion of the initialization failed
254 private static boolean loadClasses() {
258 Class aeTargetClass = Class.forName("com.apple.MacOS.AETarget");
259 Class osUtilsClass = Class.forName("com.apple.MacOS.OSUtils");
260 Class appleEventClass = Class.forName("com.apple.MacOS.AppleEvent");
261 Class aeClass = Class.forName("com.apple.MacOS.ae");
262 aeDescClass = Class.forName("com.apple.MacOS.AEDesc");
264 aeTargetConstructor = aeTargetClass.getDeclaredConstructor(new Class [] { int.class });
265 appleEventConstructor = appleEventClass.getDeclaredConstructor(new Class[] { int.class, int.class, aeTargetClass, int.class, int.class });
266 aeDescConstructor = aeDescClass.getDeclaredConstructor(new Class[] { String.class });
268 makeOSType = osUtilsClass.getDeclaredMethod("makeOSType", new Class [] { String.class });
269 putParameter = appleEventClass.getDeclaredMethod("putParameter", new Class[] { int.class, aeDescClass });
270 sendNoReply = appleEventClass.getDeclaredMethod("sendNoReply", new Class[] { });
272 Field keyDirectObjectField = aeClass.getDeclaredField("keyDirectObject");
273 keyDirectObject = (Integer) keyDirectObjectField.get(null);
274 Field autoGenerateReturnIDField = appleEventClass.getDeclaredField("kAutoGenerateReturnID");
275 kAutoGenerateReturnID = (Integer) autoGenerateReturnIDField.get(null);
276 Field anyTransactionIDField = appleEventClass.getDeclaredField("kAnyTransactionID");
277 kAnyTransactionID = (Integer) anyTransactionIDField.get(null);
278 } catch (ClassNotFoundException cnfe) {
279 errorMessage = cnfe.getMessage();
281 } catch (NoSuchMethodException nsme) {
282 errorMessage = nsme.getMessage();
284 } catch (NoSuchFieldException nsfe) {
285 errorMessage = nsfe.getMessage();
287 } catch (IllegalAccessException iae) {
288 errorMessage = iae.getMessage();
294 mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils");
295 mrjOSTypeClass = Class.forName("com.apple.mrj.MRJOSType");
296 Field systemFolderField = mrjFileUtilsClass.getDeclaredField("kSystemFolderType");
297 kSystemFolderType = systemFolderField.get(null);
298 findFolder = mrjFileUtilsClass.getDeclaredMethod("findFolder", new Class[] { mrjOSTypeClass });
299 getFileCreator = mrjFileUtilsClass.getDeclaredMethod("getFileCreator", new Class[] { File.class });
300 getFileType = mrjFileUtilsClass.getDeclaredMethod("getFileType", new Class[] { File.class });
301 } catch (ClassNotFoundException cnfe) {
302 errorMessage = cnfe.getMessage();
304 } catch (NoSuchFieldException nsfe) {
305 errorMessage = nsfe.getMessage();
307 } catch (NoSuchMethodException nsme) {
308 errorMessage = nsme.getMessage();
310 } catch (SecurityException se) {
311 errorMessage = se.getMessage();
313 } catch (IllegalAccessException iae) {
314 errorMessage = iae.getMessage();
320 Class linker = Class.forName("com.apple.mrj.jdirect.Linker");
321 Constructor constructor = linker.getConstructor(new Class[]{ Class.class });
322 linkage = constructor.newInstance(new Object[] { BrowserLauncher.class });
323 } catch (ClassNotFoundException cnfe) {
324 errorMessage = cnfe.getMessage();
326 } catch (NoSuchMethodException nsme) {
327 errorMessage = nsme.getMessage();
329 } catch (InvocationTargetException ite) {
330 errorMessage = ite.getMessage();
332 } catch (InstantiationException ie) {
333 errorMessage = ie.getMessage();
335 } catch (IllegalAccessException iae) {
336 errorMessage = iae.getMessage();
342 mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils");
343 openURL = mrjFileUtilsClass.getDeclaredMethod("openURL", new Class[] { String.class });
344 } catch (ClassNotFoundException cnfe) {
345 errorMessage = cnfe.getMessage();
347 } catch (NoSuchMethodException nsme) {
348 errorMessage = nsme.getMessage();
359 * Attempts to locate the default web browser on the local system. Caches results so it
360 * only locates the browser once for each use of this class per JVM instance.
361 * @return The browser for the system. Note that this may not be what you would consider
362 * to be a standard web browser; instead, it's the application that gets called to
363 * open the default web browser. In some cases, this will be a non-String object
364 * that provides the means of calling the default browser.
366 private static Object locateBrowser() {
367 if (browser != null) {
373 Integer finderCreatorCode = (Integer) makeOSType.invoke(null, new Object[] { FINDER_CREATOR });
374 Object aeTarget = aeTargetConstructor.newInstance(new Object[] { finderCreatorCode });
375 Integer gurlType = (Integer) makeOSType.invoke(null, new Object[] { GURL_EVENT });
376 Object appleEvent = appleEventConstructor.newInstance(new Object[] { gurlType, gurlType, aeTarget, kAutoGenerateReturnID, kAnyTransactionID });
377 // Don't set browser = appleEvent because then the next time we call
378 // locateBrowser(), we'll get the same AppleEvent, to which we'll already have
379 // added the relevant parameter. Instead, regenerate the AppleEvent every time.
380 // There's probably a way to do this better; if any has any ideas, please let
383 } catch (IllegalAccessException iae) {
385 errorMessage = iae.getMessage();
387 } catch (InstantiationException ie) {
389 errorMessage = ie.getMessage();
391 } catch (InvocationTargetException ite) {
393 errorMessage = ite.getMessage();
399 systemFolder = (File) findFolder.invoke(null, new Object[] { kSystemFolderType });
400 } catch (IllegalArgumentException iare) {
402 errorMessage = iare.getMessage();
404 } catch (IllegalAccessException iae) {
406 errorMessage = iae.getMessage();
408 } catch (InvocationTargetException ite) {
410 errorMessage = ite.getTargetException().getClass() + ": " + ite.getTargetException().getMessage();
413 String[] systemFolderFiles = systemFolder.list();
414 // Avoid a FilenameFilter because that can't be stopped mid-list
415 for(int i = 0; i < systemFolderFiles.length; i++) {
417 File file = new File(systemFolder, systemFolderFiles[i]);
418 if (!file.isFile()) {
421 // We're looking for a file with a creator code of 'MACS' and
422 // a type of 'FNDR'. Only requiring the type results in non-Finder
423 // applications being picked up on certain Mac OS 9 systems,
424 // especially German ones, and sending a GURL event to those
425 // applications results in a logout under Multiple Users.
426 Object fileType = getFileType.invoke(null, new Object[] { file });
427 if (FINDER_TYPE.equals(fileType.toString())) {
428 Object fileCreator = getFileCreator.invoke(null, new Object[] { file });
429 if (FINDER_CREATOR.equals(fileCreator.toString())) {
430 browser = file.toString(); // Actually the Finder, but that's OK
434 } catch (IllegalArgumentException iare) {
436 errorMessage = iare.getMessage();
438 } catch (IllegalAccessException iae) {
440 errorMessage = iae.getMessage();
442 } catch (InvocationTargetException ite) {
444 errorMessage = ite.getTargetException().getClass() + ": " + ite.getTargetException().getMessage();
452 browser = ""; // Return something non-null
458 browser = "command.com";
462 browser = "netscape";
469 * Attempts to open the default web browser to the given URL.
470 * @param url The URL to open
471 * @throws IOException If the web browser could not be located or does not run
473 public static void openURL(String url) throws IOException {
474 if (!loadedWithoutErrors) {
475 throw new IOException("Exception in finding browser: " + errorMessage);
477 Object browser = locateBrowser();
478 if (browser == null) {
479 throw new IOException("Unable to locate browser: " + errorMessage);
484 Object aeDesc = null;
486 aeDesc = aeDescConstructor.newInstance(new Object[] { url });
487 putParameter.invoke(browser, new Object[] { keyDirectObject, aeDesc });
488 sendNoReply.invoke(browser, new Object[] { });
489 } catch (InvocationTargetException ite) {
490 throw new IOException("InvocationTargetException while creating AEDesc: " + ite.getMessage());
491 } catch (IllegalAccessException iae) {
492 throw new IOException("IllegalAccessException while building AppleEvent: " + iae.getMessage());
493 } catch (InstantiationException ie) {
494 throw new IOException("InstantiationException while creating AEDesc: " + ie.getMessage());
496 aeDesc = null; // Encourage it to get disposed if it was created
497 browser = null; // Ditto
501 Runtime.getRuntime().exec(new String[] { (String) browser, url } );
504 int[] instance = new int[1];
505 int result = ICStart(instance, 0);
507 int[] selectionStart = new int[] { 0 };
508 byte[] urlBytes = url.getBytes();
509 int[] selectionEnd = new int[] { urlBytes.length };
510 result = ICLaunchURL(instance[0], new byte[] { 0 }, urlBytes,
511 urlBytes.length, selectionStart,
514 // Ignore the return value; the URL was launched successfully
515 // regardless of what happens here.
518 throw new IOException("Unable to launch URL: " + result);
521 throw new IOException("Unable to create an Internet Config instance: " + result);
526 openURL.invoke(null, new Object[] { url });
527 } catch (InvocationTargetException ite) {
528 throw new IOException("InvocationTargetException while calling openURL: " + ite.getMessage());
529 } catch (IllegalAccessException iae) {
530 throw new IOException("IllegalAccessException while calling openURL: " + iae.getMessage());
535 // Add quotes around the URL to allow ampersands and other special
536 // characters to work.
537 Process process = Runtime.getRuntime().exec(new String[] { (String) browser,
538 FIRST_WINDOWS_PARAMETER,
539 SECOND_WINDOWS_PARAMETER,
540 THIRD_WINDOWS_PARAMETER,
542 // This avoids a memory leak on some versions of Java on Windows.
543 // That's hinted at in <http://developer.java.sun.com/developer/qow/archive/68/>.
547 } catch (InterruptedException ie) {
548 throw new IOException("InterruptedException while launching browser: " + ie.getMessage());
552 // Assume that we're on Unix and that Netscape is installed
554 // First, attempt to open the URL in a currently running session of Netscape
555 process = Runtime.getRuntime().exec(new String[] { (String) browser,
556 NETSCAPE_REMOTE_PARAMETER,
557 NETSCAPE_OPEN_PARAMETER_START +
559 NETSCAPE_OPEN_PARAMETER_END });
561 int exitCode = process.waitFor();
562 if (exitCode != 0) { // if Netscape was not open
563 Runtime.getRuntime().exec(new String[] { (String) browser, url });
565 } catch (InterruptedException ie) {
566 throw new IOException("InterruptedException while launching browser: " + ie.getMessage());
570 // This should never occur, but if it does, we'll try the simplest thing possible
571 Runtime.getRuntime().exec(new String[] { (String) browser, url });
577 * Methods required for Mac OS X. The presence of native methods does not cause
578 * any problems on other platforms.
580 private native static int ICStart(int[] instance, int signature);
581 private native static int ICStop(int[] instance);
582 private native static int ICLaunchURL(int instance, byte[] hint, byte[] data, int len,
583 int[] selectionStart, int[] selectionEnd);