本文主要介紹的是利用java程序打開指定某個(gè)的瀏覽器
方法一:
- import java.lang.reflect.Method;
- //實(shí)現(xiàn)打開瀏覽器并跳到指定網(wǎng)址的類
- public class TestUrl {
- public static void openURL(String url) {
- try {
- browse(url);
- } catch (Exception e) {
- }
- }
- private static void browse(String url) throws Exception {
- // 獲取操作系統(tǒng)的名字
- 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")) {
- // windows的打開方式。
- Runtime.getRuntime().exec(
- "rundll32 url.dll,FileProtocolHandler " + url);
- } else {
- // Unix or Linux的打開方式
- String[] browsers = { "firefox", "opera", "konqueror", "epiphany",
- "mozilla", "netscape" };
- String browser = null;
- for (int count = 0; count < browsers.length && browser == null; count++) {
- // 執(zhí)行代碼,在brower有值后跳出,
- // 這里是如果進(jìn)程創(chuàng)建成功了,==0是表示正常結(jié)束。
- if (Runtime.getRuntime()
- .exec(new String[] { "which", browsers[count] })
- .waitFor() == 0)
- browser = browsers[count];
- if (browser == null)
- throw new Exception("Could not find web browser");
- else
- // 這個(gè)值在上面已經(jīng)成功的得到了一個(gè)進(jìn)程。
- Runtime.getRuntime().exec(new String[] { browser, url });
- }
- // 主方法 測(cè)試類
- }
- }
- public static void main(String[] args) {
- // 這里填寫你的網(wǎng)址
- String url = "xxx";
- openURL(url);
- }
- }
方法二:
使用默認(rèn)瀏覽器打開:
- String site = "www.baidu.com";
- try {
- Desktop desktop = Desktop.getDesktop();
- if (desktop.isDesktopSupported()
- && desktop.isSupported(Desktop.Action.BROWSE)) {
- URI uri = new URI(site);
- desktop.browse(uri);
- }
- } catch (IOException ex) {
- System.out.println(ex);
- } catch (URISyntaxException ex) {
- System.out.println(ex);
- }
方法三:
通過獲取環(huán)境變量的瀏覽器路徑,然后啟動(dòng)瀏覽器
- 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")) {
- firefox = value;
- break;
- }
- }
- 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 > |
轉(zhuǎn)載請(qǐng)注明出處:http://blog.csdn.net/l1028386804/article/details/49334975
- package com.lyz.test;
- import java.awt.Desktop;
- import java.io.IOException;
- import java.net.URI;
- import java.net.URISyntaxException;
- /**
- * @author liuyazhuang
- * @time 2015-10-22
- *
- */
- public class Gotourl {
- /**
- * 打開IE瀏覽器訪問頁面
- */
- public static void openIEBrowser(){
- //啟用cmd運(yùn)行IE的方式來打開網(wǎng)址。
- String str = "cmd /c start iexplore http://blog.csdn.net/l1028386804";
- try {
- Runtime.getRuntime().exec(str);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 打開默認(rèn)瀏覽器訪問頁面
- */
- public static void openDefaultBrowser(){
- //啟用系統(tǒng)默認(rèn)瀏覽器來打開網(wǎng)址。
- try {
- URI uri = new URI("http://blog.csdn.net/l1028386804");
- Desktop.getDesktop().browse(uri);
- } catch (URISyntaxException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- openIEBrowser();
- openDefaultBrowser();
- }
- }
聯(lián)系客服