本文主要介紹的是利用java程序打開指定某個的瀏覽器
方法一:
import java.lang.reflect.Method; //實現(xiàn)打開瀏覽器并跳到指定網(wǎng)址的類 public static void openURL(String url) { private static void browse(String url) throws Exception { String osName = System.getProperty("os.name", ""); if (osName.startsWith("Mac OS")) { Class fileMgr = Class.forName("com.apple.eio.FileManager"); Method openURL = fileMgr.getDeclaredMethod("openURL",new Class[] { String.class }); openURL.invoke(null, new Object[] { url }); } else if (osName.startsWith("Windows")) { Runtime.getRuntime().exec( "rundll32 url.dll,FileProtocolHandler " + url); String[] browsers = { "firefox", "opera", "konqueror", "epiphany", for (int count = 0; count < browsers.length && browser == null; count++) { // 執(zhí)行代碼,在brower有值后跳出, // 這里是如果進程創(chuàng)建成功了,==0是表示正常結束。 .exec(new String[] { "which", browsers[count] }) browser = browsers[count]; throw new Exception("Could not find web browser"); // 這個值在上面已經(jīng)成功的得到了一個進程。 Runtime.getRuntime().exec(new String[] { browser, url }); public static void main(String[] args) {
方法二:
使用默認瀏覽器打開:
String site = "www.baidu.com"; Desktop desktop = Desktop.getDesktop(); if (desktop.isDesktopSupported() && desktop.isSupported(Desktop.Action.BROWSE)) { } catch (IOException ex) { } catch (URISyntaxException ex) {
方法三:
通過獲取環(huán)境變量的瀏覽器路徑,然后啟動瀏覽器
String firefox = "C:\\Program Files\\Mozilla Firefox\\firefox.exe"; Map map = System.getenv(); for (Iterator itr = map.keySet().iterator(); itr.hasNext();) { String value = (String) map.get((String) itr.next()); if (value.contains("firefox.exe")) { Runtime.getRuntime().exec(new String[] { firefox, "www.baidu.com" });
方法四:
js方式:
< script type = "text/javascript" >
window.onload=function(){
var WSH = new ActiveXObject("WScript.Shell");
WSH.Run("chrome.exe www.baidu.com");
}
</ script >
|
// 自動關閉瀏覽器
Runtime.getRuntime().exec("taskkill /F /IM 360se.exe");
轉(zhuǎn)載請注明出處:http://blog.csdn.net/l1028386804/article/details/49334975
import java.io.IOException; import java.net.URISyntaxException; public static void openIEBrowser(){ //啟用cmd運行IE的方式來打開網(wǎng)址。 String str = "cmd /c start iexplore http://blog.csdn.net/l1028386804"; Runtime.getRuntime().exec(str); } catch (IOException e) { public static void openDefaultBrowser(){ //啟用系統(tǒng)默認瀏覽器來打開網(wǎng)址。 URI uri = new URI("http://blog.csdn.net/l1028386804"); Desktop.getDesktop().browse(uri); } catch (URISyntaxException e) { } catch (IOException e) { public static void main(String[] args) {
|