📄 browserlauncher.java
字号:
String[] systemFolderFiles = systemFolder.list();
// Avoid a FilenameFilter because that can't be stopped mid-list
for (int i = 0; i < systemFolderFiles.length; i++) {
try {
File file = new File(systemFolder, systemFolderFiles[i]);
if (!file.isFile()) {
continue;
}
// We're looking for a file with a creator code of
// 'MACS' and
// a type of 'FNDR'. Only requiring the type results in
// non-Finder
// applications being picked up on certain Mac OS 9
// systems,
// especially German ones, and sending a GURL event to
// those
// applications results in a logout under Multiple
// Users.
Object fileType = getFileType.invoke(null, new Object[] {
file
});
if (FINDER_TYPE.equals(fileType.toString())) {
Object fileCreator = getFileCreator.invoke(null, new Object[] {
file
});
if (FINDER_CREATOR.equals(fileCreator.toString())) {
browser = file.toString(); // Actually the
// Finder, but
// that's OK
return browser;
}
}
} catch (IllegalArgumentException iare) {
browser = null;
errorMessage = iare.getMessage();
return null;
} catch (IllegalAccessException iae) {
browser = null;
errorMessage = iae.getMessage();
return browser;
} catch (InvocationTargetException ite) {
browser = null;
errorMessage = ite.getTargetException().getClass() + ": " + ite.getTargetException().getMessage();
return browser;
}
}
browser = null;
break;
case MRJ_3_0:
case MRJ_3_1:
browser = ""; // Return something non-null
break;
case WINDOWS_NT:
browser = "cmd.exe";
break;
case WINDOWS_9x:
browser = "command.com";
break;
case OTHER:
default:
// On systems other than Windows and the Mac, we try via a
// rather Unix-
// specific hack to read the BROWSER environment variable
// <http://tuxedo.org/~esr/BROWSER/>. If we can't read that
// variable or
// it isn't set, we use Netscape.
// Note: This is commented out for now. It'll work soon.
browser = getEnvironmentBrowser();
if (browser == null) {
browser = "netscape";
}
break;
}
return browser;
}
/**
* Attempts to open the default web browser to the given URL.
*
* @param url The URL to open
* @throws IOException If the web browser could not be located or does not
* run
*/
public static void openURL(String url) throws IOException {
if (!loadedWithoutErrors) {
throw new IOException("Exception in finding browser: " + errorMessage);
}
if (browserCommand != null) {
String[] cmd = Util.splitString(browserCommand, ' ', '"', '\\');
String[] cmde = new String[cmd.length + 1];
System.arraycopy(cmd, 0, cmde, 0, cmd.length);
cmde[cmd.length] = url;
Runtime.getRuntime().exec(cmde);
return;
}
Object browser = locateBrowser();
if (browser == null) {
throw new IOException("Unable to locate browser: " + errorMessage);
}
switch (jvm) {
case MRJ_2_0:
Object aeDesc = null;
try {
aeDesc = aeDescConstructor.newInstance(new Object[] {
url
});
putParameter.invoke(browser, new Object[] {
keyDirectObject, aeDesc
});
sendNoReply.invoke(browser, new Object[] {});
} catch (InvocationTargetException ite) {
throw new IOException("InvocationTargetException while creating AEDesc: " + ite.getMessage());
} catch (IllegalAccessException iae) {
throw new IOException("IllegalAccessException while building AppleEvent: " + iae.getMessage());
} catch (InstantiationException ie) {
throw new IOException("InstantiationException while creating AEDesc: " + ie.getMessage());
} finally {
aeDesc = null; // Encourage it to get disposed if it was
// created
browser = null; // Ditto
}
break;
case MRJ_2_1:
Runtime.getRuntime().exec(new String[] {
(String) browser, url
});
break;
case MRJ_3_0:
int[] instance = new int[1];
int result = ICStart(instance, 0);
if (result == 0) {
int[] selectionStart = new int[] {
0
};
byte[] urlBytes = url.getBytes();
int[] selectionEnd = new int[] {
urlBytes.length
};
result = ICLaunchURL(instance[0], new byte[] {
0
}, urlBytes, urlBytes.length, selectionStart, selectionEnd);
if (result == 0) {
// Ignore the return value; the URL was launched
// successfully
// regardless of what happens here.
ICStop(instance);
} else {
throw new IOException("Unable to launch URL: " + result);
}
} else {
throw new IOException("Unable to create an Internet Config instance: " + result);
}
break;
case MRJ_3_1:
case MRJ_COCOA:
try {
openURL.invoke(null, new Object[] {
url
});
} catch (InvocationTargetException ite) {
throw new IOException("InvocationTargetException while calling openURL: " + ite.getMessage());
} catch (IllegalAccessException iae) {
throw new IOException("IllegalAccessException while calling openURL: " + iae.getMessage());
}
break;
case WINDOWS_NT:
case WINDOWS_9x:
// Add quotes around the URL to allow ampersands and other
// special
// characters to work.
String[] arguments;
if (jvm == WINDOWS_9x) {
arguments = new String[] {
(String) browser, FIRST_WINDOWS_PARAMETER, SECOND_WINDOWS_PARAMETER, null
};
} else {
arguments = new String[] {
(String) browser, FIRST_WINDOWS_PARAMETER, SECOND_WINDOWS_PARAMETER, THIRD_WINDOWS_PARAMETER,
null
};
}
arguments[arguments.length - 1] = '"' + url + '"';
Process process = Runtime.getRuntime().exec(arguments);
// This avoids a memory leak on some versions of Java on
// Windows.
// That's hinted at in
// <http://developer.java.sun.com/developer/qow/archive/68/>.
try {
process.waitFor();
process.exitValue();
} catch (InterruptedException ie) {
throw new IOException("InterruptedException while launching browser: " + ie.getMessage());
}
break;
case OTHER:
// Assume that we're on Unix and that Netscape is installed
// Attempt to open the URL in a currently running session of
// Netscape
process = Runtime.getRuntime().exec(
new String[] {
(String) browser, NETSCAPE_REMOTE_PARAMETER,
NETSCAPE_OPEN_PARAMETER_START + url + NETSCAPE_OPEN_PARAMETER_END
});
try {
int exitCode = process.waitFor();
if (exitCode != 0) { // if Netscape was not open
Runtime.getRuntime().exec(new String[] {
(String) browser, url
});
}
} catch (InterruptedException ie) {
throw new IOException("InterruptedException while launching browser: " + ie.getMessage());
}
break;
default:
// This should never occur, but if it does, we'll try the
// simplest thing
// possible
Runtime.getRuntime().exec(new String[] {
(String) browser, url
});
break;
}
}
/**
* Tries to read the BROWSER environment variable, which should be set to
* the absolute path of the user's preferred web browser as proposed at
* <http://tuxedo.org/~esr/BROWSER/>.
*
* @return The value of the BROWSER environment variable, or null if the
* variable does not exist or can't be read.
*/
private static String getEnvironmentBrowser() {
String browser = null;
try {
String[] echoParams = {
"/bin/sh", "-c", "echo ${BROWSER:-netscape}"
};
Process echoProcess = Runtime.getRuntime().exec(echoParams);
InputStream echoStream = echoProcess.getInputStream();
BufferedReader echoReader = new BufferedReader(new InputStreamReader(echoStream));
browser = echoReader.readLine();
echoReader.close();
} catch (Throwable t) {
// If anything goes wrong, we'll return null.
}
return browser;
}
/*
* Methods required for Mac OS X 10.0.x. The presence of native methods does
* not cause any problems on other platforms.
*/
private native static int ICStart(int[] instance, int signature);
private native static int ICStop(int[] instance);
private native static int ICLaunchURL(int instance, byte[] hint, byte[] data, int len, int[] selectionStart, int[] selectionEnd);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -