![]() |
Quadcap Embeddable Server |
It may support other protocols depending on the system -- mailto, ftp, etc. -- but that has not been rigorously tested and is not guaranteed to work.
Yes, this is platform-specific code, and yes, it may rely on classes on certain platforms that are not part of the standard JDK. What we're trying to do, though, is to take something that's frequently desirable but inherently platform-specific -- opening a default browser -- and allow programmers (you, for example) to do so without worrying about dropping into native code or doing anything else similarly evil.
Anyway, this code is completely in Java and will run on all JDK 1.1-compliant systems without modification or a need for additional libraries. All classes that are required on certain platforms to allow this to run are dynamically loaded at runtime via reflection and, if not found, will not cause this to do anything other than returning an error when opening the browser.
There are certain system requirements for this class, as it's running through Runtime.exec(), which is Java's way of making a native system call. Currently, this requires that a Macintosh have a Finder which supports the GURL event, which is true for Mac OS 8.0 and 8.1 systems that have the Internet Scripting AppleScript dictionary installed in the Scripting Additions folder in the Extensions folder (which is installed by default as far as I know under Mac OS 8.0 and 8.1), and for all Mac OS 8.5 and later systems. On Windows, it only runs under Win32 systems (Windows 95, 98, and NT 4.0, as well as later versions of all). On other systems, this drops back from the inherently platform-sensitive concept of a default browser and simply attempts to launch Netscape via a shell command.
This code is Copyright 1999-2001 by Eric Albert (ejalbert@cs.stanford.edu) and may be redistributed or modified in any form without restrictions as long as the portion of this comment from this paragraph through the end of the comment is not removed. The author requests that he be notified of any application, applet, or other binary that makes use of this code, but that's more out of curiosity than anything and is not required. This software includes no warranty. The author is not repsonsible for any loss of data or functionality or any adverse or unexpected effects of using this software.
Credits:
Steven Spencer, JavaWorld magazine (Java Tip 66 )
Thanks also to Ron B. Yeh, Eric Shapiro, Ben Engber, Paul Teitlebaum, Andrea Cantatore, Larry Barowski, Trevor Bedzek, Frank Miedrich, and Ron Rabakukk
Definition at line 87 of file BrowserLauncher.java.
Static Public Member Functions | |
| void | openURL (String url) throws IOException |
| Attempts to open the default web browser to the given URL. | |
Static Package Functions | |
| [static initializer] | |
Private Member Functions | |
| BrowserLauncher () | |
| This class should be never be instantiated; this just ensures so. | |
Static Private Member Functions | |
| native int | ICLaunchURL (int instance, byte[] hint, byte[] data, int len, int[] selectionStart, int[] selectionEnd) |
| native int | ICStart (int[] instance, int signature) |
| Methods required for Mac OS X. | |
| native int | ICStop (int[] instance) |
| boolean | loadClasses () |
| Called by a static initializer to load any classes, fields, and methods required at runtime to locate the user's web browser. | |
| Object | locateBrowser () |
| Attempts to locate the default web browser on the local system. | |
Static Private Attributes | |
| final String | FINDER_CREATOR = "MACS" |
| The creator code of the Finder on a Macintosh, which is needed to send AppleEvents to the application. | |
| final String | FINDER_TYPE = "FNDR" |
| The file type of the Finder on a Macintosh. | |
| final String | FIRST_WINDOWS_PARAMETER = "/c" |
| The first parameter that needs to be passed into Runtime.exec() to open the default web browser on Windows. | |
| final String | GURL_EVENT = "GURL" |
| The name for the AppleEvent type corresponding to a GetURL event. | |
| final String | JDirect_MacOSX = "/System/Library/Frameworks/Carbon.framework/Frameworks/HIToolbox.framework/HIToolbox" |
| The framework to reference on Mac OS X. | |
| final int | MRJ_2_0 = 0 |
| JVM constant for MRJ 2.0. | |
| final int | MRJ_2_1 = 1 |
| JVM constant for MRJ 2.1 or later. | |
| final int | MRJ_3_0 = 3 |
| JVM constant for Java on Mac OS X 10.0 (MRJ 3.0). | |
| final int | MRJ_3_1 = 4 |
| JVM constant for MRJ 3.1. | |
| final String | NETSCAPE_OPEN_PARAMETER_END = ")'" |
| final String | NETSCAPE_OPEN_PARAMETER_START = "'openURL(" |
| final String | NETSCAPE_REMOTE_PARAMETER = "-remote" |
| The shell parameters for Netscape that opens a given URL in an already-open copy of Netscape on many command-line systems. | |
| final int | OTHER = -1 |
| JVM constant for any other platform. | |
| final String | SECOND_WINDOWS_PARAMETER = "start" |
| The second parameter for Runtime.exec() on Windows. | |
| final String | THIRD_WINDOWS_PARAMETER = "\"\"" |
| The third parameter for Runtime.exec() on Windows. | |
| final int | WINDOWS_9x = 6 |
| JVM constant for any Windows 9x JVM. | |
| final int | WINDOWS_NT = 5 |
| JVM constant for any Windows NT JVM. | |
| Class | aeDescClass |
| The com.apple.MacOS.AEDesc class. | |
| Constructor | aeDescConstructor |
| The <init>(String) method of com.apple.MacOS.AEDesc. | |
| Constructor | aeTargetConstructor |
| The <init>(int) method of com.apple.MacOS.AETarget. | |
| Constructor | appleEventConstructor |
| The <init>(int, int, int) method of com.apple.MacOS.AppleEvent. | |
| Object | browser |
| The browser for the system. | |
| String | errorMessage |
| The message from any exception thrown throughout the initialization process. | |
| Method | findFolder |
| The findFolder method of com.apple.mrj.MRJFileUtils. | |
| Method | getFileCreator |
| The getFileCreator method of com.apple.mrj.MRJFileUtils. | |
| Method | getFileType |
| The getFileType method of com.apple.mrj.MRJFileUtils. | |
| int | jvm |
| The Java virtual machine that we are running on. | |
| Integer | kAnyTransactionID |
| The kAnyTransactionID AppleEvent code. | |
| Integer | kAutoGenerateReturnID |
| The kAutoGenerateReturnID AppleEvent code. | |
| Object | kSystemFolderType |
| Actually an MRJOSType pointing to the System Folder on a Macintosh. | |
| Integer | keyDirectObject |
| The keyDirectObject AppleEvent parameter type. | |
| Object | linkage |
| The linkage object required for JDirect 3 on Mac OS X. | |
| boolean | loadedWithoutErrors |
| Caches whether any classes, methods, and fields that are not part of the JDK and need to be dynamically loaded at runtime loaded successfully. | |
| Method | makeOSType |
| The makeOSType method of com.apple.MacOS.OSUtils. | |
| Class | mrjFileUtilsClass |
| The com.apple.mrj.MRJFileUtils class. | |
| Class | mrjOSTypeClass |
| The com.apple.mrj.MRJOSType class. | |
| Method | openURL |
| The openURL method of com.apple.mrj.MRJFileUtils. | |
| Method | putParameter |
| The putParameter method of com.apple.MacOS.AppleEvent. | |
| Method | sendNoReply |
| The sendNoReply method of com.apple.MacOS.AppleEvent. | |
|
|
This class should be never be instantiated; this just ensures so.
Definition at line 294 of file BrowserLauncher.java. |
|
|
|
|
||||||||||||||||||||||||||||
|
Referenced by luxor.browser.albert.BrowserLauncher.openURL(). |
|
||||||||||||
|
Methods required for Mac OS X. The presence of native methods does not cause any problems on other platforms. Referenced by luxor.browser.albert.BrowserLauncher.openURL(). |
|
|
Referenced by luxor.browser.albert.BrowserLauncher.openURL(). |
|
|
Called by a static initializer to load any classes, fields, and methods required at runtime to locate the user's web browser.
Definition at line 458 of file BrowserLauncher.java. References luxor.browser.albert.BrowserLauncher.aeDescClass, luxor.browser.albert.BrowserLauncher.aeDescConstructor, luxor.browser.albert.BrowserLauncher.aeTargetConstructor, luxor.browser.albert.BrowserLauncher.appleEventConstructor, luxor.browser.albert.BrowserLauncher.jvm, luxor.browser.albert.BrowserLauncher.kAnyTransactionID, luxor.browser.albert.BrowserLauncher.kAutoGenerateReturnID, luxor.browser.albert.BrowserLauncher.keyDirectObject, luxor.browser.albert.BrowserLauncher.makeOSType, luxor.browser.albert.BrowserLauncher.MRJ_2_0, luxor.browser.albert.BrowserLauncher.putParameter, and luxor.browser.albert.BrowserLauncher.sendNoReply. |
|
|
Attempts to locate the default web browser on the local system. Caches results so it only locates the browser once for each use of this class per JVM instance.
Definition at line 611 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.openURL(). |
|
|
|
The com.apple.MacOS.AEDesc class.
Definition at line 176 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.loadClasses(). |
|
|
The <init>(String) method of com.apple.MacOS.AEDesc.
Definition at line 182 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.loadClasses(), and luxor.browser.albert.BrowserLauncher.openURL(). |
|
|
The <init>(int) method of com.apple.MacOS.AETarget.
Definition at line 187 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.loadClasses(). |
|
|
The <init>(int, int, int) method of com.apple.MacOS.AppleEvent.
Definition at line 192 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.loadClasses(). |
|
|
The browser for the system.
Definition at line 197 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.openURL(). |
|
|
The message from any exception thrown throughout the initialization process.
Definition at line 203 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.openURL(). |
|
|
The creator code of the Finder on a Macintosh, which is needed to send AppleEvents to the application.
Definition at line 94 of file BrowserLauncher.java. |
|
|
The file type of the Finder on a Macintosh. Hardcoding "Finder" would keep non-U.S. English systems from working properly. Definition at line 100 of file BrowserLauncher.java. |
|
|
The findFolder method of com.apple.mrj.MRJFileUtils.
Definition at line 208 of file BrowserLauncher.java. |
|
|
The first parameter that needs to be passed into Runtime.exec() to open the default web browser on Windows.
Definition at line 106 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.openURL(). |
|
|
The getFileCreator method of com.apple.mrj.MRJFileUtils.
Definition at line 213 of file BrowserLauncher.java. |
|
|
The getFileType method of com.apple.mrj.MRJFileUtils.
Definition at line 218 of file BrowserLauncher.java. |
|
|
The name for the AppleEvent type corresponding to a GetURL event.
Definition at line 111 of file BrowserLauncher.java. |
|
|
The framework to reference on Mac OS X.
Definition at line 116 of file BrowserLauncher.java. |
|
|
The Java virtual machine that we are running on. Actually, in most cases we only care about the operating system, but some operating systems require us to switch on the VM. Definition at line 225 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.loadClasses(), and luxor.browser.albert.BrowserLauncher.openURL(). |
|
|
The kAnyTransactionID AppleEvent code.
Definition at line 230 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.loadClasses(). |
|
|
The kAutoGenerateReturnID AppleEvent code.
Definition at line 235 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.loadClasses(). |
|
|
The keyDirectObject AppleEvent parameter type.
Definition at line 245 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.loadClasses(), and luxor.browser.albert.BrowserLauncher.openURL(). |
|
|
Actually an MRJOSType pointing to the System Folder on a Macintosh.
Definition at line 240 of file BrowserLauncher.java. |
|
|
The linkage object required for JDirect 3 on Mac OS X.
Definition at line 250 of file BrowserLauncher.java. |
|
|
Caches whether any classes, methods, and fields that are not part of the JDK and need to be dynamically loaded at runtime loaded successfully.
Note that if this is Definition at line 259 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.openURL(). |
|
|
The makeOSType method of com.apple.MacOS.OSUtils.
Definition at line 264 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.loadClasses(). |
|
|
JVM constant for MRJ 2.0.
Definition at line 121 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.loadClasses(), and luxor.browser.albert.BrowserLauncher.openURL(). |
|
|
JVM constant for MRJ 2.1 or later.
Definition at line 126 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.openURL(). |
|
|
JVM constant for Java on Mac OS X 10.0 (MRJ 3.0).
Definition at line 131 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.openURL(). |
|
|
JVM constant for MRJ 3.1.
Definition at line 136 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.openURL(). |
|
|
The com.apple.mrj.MRJFileUtils class.
Definition at line 269 of file BrowserLauncher.java. |
|
|
The com.apple.mrj.MRJOSType class.
Definition at line 274 of file BrowserLauncher.java. |
|
|
Definition at line 137 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.openURL(). |
|
|
Definition at line 138 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.openURL(). |
|
|
The shell parameters for Netscape that opens a given URL in an already-open copy of Netscape on many command-line systems.
Definition at line 144 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.openURL(). |
|
|
The openURL method of com.apple.mrj.MRJFileUtils.
Definition at line 279 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.openURL(). |
|
|
JVM constant for any other platform.
Definition at line 149 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.openURL(). |
|
|
The putParameter method of com.apple.MacOS.AppleEvent.
Definition at line 284 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.loadClasses(), and luxor.browser.albert.BrowserLauncher.openURL(). |
|
|
The second parameter for Runtime.exec() on Windows.
Definition at line 154 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.openURL(). |
|
|
The sendNoReply method of com.apple.MacOS.AppleEvent.
Definition at line 289 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.loadClasses(), and luxor.browser.albert.BrowserLauncher.openURL(). |
|
|
The third parameter for Runtime.exec() on Windows. This is a "title" parameter that the command line expects. Setting this parameter allows URLs containing spaces to work. Definition at line 161 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.openURL(). |
|
|
JVM constant for any Windows 9x JVM.
Definition at line 166 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.openURL(). |
|
|
JVM constant for any Windows NT JVM.
Definition at line 171 of file BrowserLauncher.java. Referenced by luxor.browser.albert.BrowserLauncher.openURL(). |