方法一
private static void browse2(String url) throws Exception {
Desktop desktop = Desktop.getDesktop();
if (Desktop.isDesktopSupported() && desktop.isSupported(Desktop.Action.BROWSE)) {
URI uri = new URI(url);
desktop.browse(uri);
}
}
方法二
private static void browse4(String url) throws Exception {
Runtime.getRuntime().exec("cmd /c start " + url);
}
方法三
private static void browse5(String path, String url) throws Exception {
ProcessBuilder proc = new ProcessBuilder(path,url);
proc.start();
}
方法四
private static void browse3(String url) throws Exception {
String firefox = "C:\\Program Files\\Mozilla Firefox\\firefox.exe";
Map map = System.getenv();
for (Object key : map.keySet()) {
String value = (String) map.get(key);
if (value.contains("firefox.exe")) {
firefox = value;
break;
}
}
Runtime.getRuntime().exec(new String[]{firefox, url});
}
方法五
private static void browse1(String url) throws Exception {
String osName = System.getProperty("os.name", "");
if (osName.startsWith("Windows")) {
Runtime.getRuntime().exec("rundll32 url.dll, FileProtocolHandler " + url);
}
else if (osName.startsWith("Mac OS")) {
Class fileMgr = Class.forName("com.apple.eio.FileManager");
Method openURL = fileMgr.getDeclaredMethod("openURL", String.class);
openURL.invoke(null, url);
}
else {
String[] browsers = {"firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape"};
String browser = null;
for (int count = 0; count < browsers.length && browser == null; count++) {
if (Runtime.getRuntime().exec(new String[]{"which", browsers[count]}).waitFor() == 0) {
browser = browsers[count];
}
}
if (browser == null) {
throw new RuntimeException("未找到任何可用的浏览器");
}
else {
Runtime.getRuntime().exec(new String[]{browser, url});
}
}
}