📄 browserlauncher.java
字号:
package com.sslexplorer.boot;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* BrowserLauncher is a class that provides one static method, openURL, which
* opens the default web browser for the current user of the system to the given
* URL. 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.
* <p>
* 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.
* <p>
* 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.
* <p>
* 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.
* <p>
* This code is Copyright 1999-2002 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.
* <p>
* Credits: <br>
* Steven Spencer, JavaWorld magazine ( <a
* href="http://www.javaworld.com/javaworld/javatips/jw-javatip66.html">Java Tip
* 66 </a>) <br>
* Thanks also to Ron B. Yeh, Eric Shapiro, Ben Engber, Paul Teitlebaum, Andrea
* Cantatore, Larry Barowski, Trevor Bedzek, Frank Miedrich, Ron Rabakukk, and
* Glenn Vanderburg
*
* @author Eric Albert ( <a
* href="mailto:ejalbert@cs.stanford.edu">ejalbert@cs.stanford.edu </a>)
* @version 1.4b2
*
* <p>
* Additional
* <p>
* <p>
* Changes made by Brett Smith ( <a href="mailto:brett@3sp.com">brett@3sp,com.
* </a> to allow a command to be specified that will always be used. Copyright
* 2004 3SP <a href="http://3sp.com">3SP </a>.
*/
public class BrowserLauncher {
/**
* 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.
*/
private static int jvm;
/** The browser for the system */
private static Object browser;
/** Browser command to always use */
private static String browserCommand;
/**
* Caches whether any classes, methods, and fields that are not part of the
* JDK and need to be dynamically loaded at runtime loaded successfully.
* <p>
* Note that if this is <code>false</code>,<code>openURL()</code> will
* always return an IOException.
*/
private static boolean loadedWithoutErrors;
/** The com.apple.mrj.MRJFileUtils class */
private static Class mrjFileUtilsClass;
/** The com.apple.mrj.MRJOSType class */
private static Class mrjOSTypeClass;
/** The com.apple.MacOS.AEDesc class */
private static Class aeDescClass;
/** The <init>(int) method of com.apple.MacOS.AETarget */
private static Constructor aeTargetConstructor;
/** The <init>(int, int, int) method of com.apple.MacOS.AppleEvent */
private static Constructor appleEventConstructor;
/** The <init>(String) method of com.apple.MacOS.AEDesc */
private static Constructor aeDescConstructor;
/** The findFolder method of com.apple.mrj.MRJFileUtils */
private static Method findFolder;
/** The getFileCreator method of com.apple.mrj.MRJFileUtils */
private static Method getFileCreator;
/** The getFileType method of com.apple.mrj.MRJFileUtils */
private static Method getFileType;
/** The openURL method of com.apple.mrj.MRJFileUtils */
private static Method openURL;
/** The makeOSType method of com.apple.MacOS.OSUtils */
private static Method makeOSType;
/** The putParameter method of com.apple.MacOS.AppleEvent */
private static Method putParameter;
/** The sendNoReply method of com.apple.MacOS.AppleEvent */
private static Method sendNoReply;
/** Actually an MRJOSType pointing to the System Folder on a Macintosh */
private static Object kSystemFolderType;
/** The keyDirectObject AppleEvent parameter type */
private static Integer keyDirectObject;
/** The kAutoGenerateReturnID AppleEvent code */
private static Integer kAutoGenerateReturnID;
/** The kAnyTransactionID AppleEvent code */
private static Integer kAnyTransactionID;
/** The linkage object required for JDirect 3 on Mac OS X */
// private static Object linkage;
/** The framework to reference on Mac OS X 10.0.x */
// private static final String JDirect_MacOSX =
// "/System/Library/Frameworks/Carbon.framework/Frameworks/HIToolbox.framework/HIToolbox";
/** JVM constant for MRJ 2.0 */
private static final int MRJ_2_0 = 0;
/** JVM constant for MRJ 2.1.x and 2.2.x */
private static final int MRJ_2_1 = 1;
/** JVM constant for Java on Mac OS X 10.0 (MRJ 3.0) */
private static final int MRJ_3_0 = 3;
/** JVM constant for Java 1.3.x on Mac OS X 10.1 and later (MRJ 3.1 and 3.2) */
private static final int MRJ_3_1 = 4;
/** JVM constant for Java 1.4.x and later on Mac OS X */
private static final int MRJ_COCOA = 5;
/** JVM constant for any Windows NT JVM */
private static final int WINDOWS_NT = 6;
/** JVM constant for any Windows 9x JVM */
private static final int WINDOWS_9x = 7;
/** JVM constant for any other platform */
private static final int OTHER = -1;
/**
* The file type of the Finder on a Macintosh. Hardcoding "Finder" would
* keep non-U.S. English systems from working properly.
*/
private static final String FINDER_TYPE = "FNDR";
/**
* The creator code of the Finder on a Macintosh, which is needed to send
* AppleEvents to the application.
*/
private static final String FINDER_CREATOR = "MACS";
/** The name for the AppleEvent type corresponding to a GetURL event. */
private static final String GURL_EVENT = "GURL";
/**
* The first parameter that needs to be passed into Runtime.exec() to open
* the default web browser on Windows.
*/
private static final String FIRST_WINDOWS_PARAMETER = "/c";
/** The second parameter for Runtime.exec() on Windows. */
private static final String SECOND_WINDOWS_PARAMETER = "start";
/**
* 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.
*/
private static final String THIRD_WINDOWS_PARAMETER = "\"\"";
/**
* The shell parameters for Netscape that opens a given URL in an
* already-open copy of Netscape on many command-line systems.
*/
private static final String NETSCAPE_REMOTE_PARAMETER = "-remote";
private static final String NETSCAPE_OPEN_PARAMETER_START = "'openURL(";
private static final String NETSCAPE_OPEN_PARAMETER_END = ")'";
/**
* The message from any exception thrown throughout the initialization
* process.
*/
private static String errorMessage;
/**
* An initialization block that determines the operating system and loads
* the necessary runtime data.
*/
static {
loadedWithoutErrors = true;
String osName = System.getProperty("os.name");
if (osName.startsWith("Mac OS")) {
String javaVersion = System.getProperty("java.version");
String majorJavaVersion = javaVersion.substring(0, 3);
try {
double version = Double.valueOf(majorJavaVersion).doubleValue();
if (version >= 1.4) {
jvm = MRJ_COCOA;
}
} catch (NumberFormatException nfe) {
// Fall through to earlier versions of Java on the Mac.
}
if (jvm != MRJ_COCOA) {
String mrjVersion = System.getProperty("mrj.version");
String majorMRJVersion = mrjVersion.substring(0, 3);
try {
double version = Double.valueOf(majorMRJVersion).doubleValue();
if (version == 2) {
jvm = MRJ_2_0;
} else if (version >= 2.1 && version < 3) {
// Assume that all post-2.1 2.x versions of MRJ work the
// same. MRJ
// 2.1 actually
// works via Runtime.exec() and 2.2 supports that but
// has an
// openURL() method
// as well that we don't use because the Runtime.exec()
// method works
// fine.
jvm = MRJ_2_1;
} else if (version == 3.0) {
jvm = MRJ_3_0;
} else if (version >= 3.1) {
// Assume that all 3.1 and later versions of MRJ work
// the same.
jvm = MRJ_3_1;
} else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -