本文主要介绍的是利用java程序打开指定某个的浏览器
方法一:
- import java.lang.reflect.Method;
- //实现打开浏览器并跳到指定网址的类
- public class TestUrl {
- public static void openURL(String url) {
- try {
- browse(url);
- } catch (Exception e) {
- }
- }
- 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")) {
- // 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++) {
- // 执行代码,在brower有值后跳出,
- // 这里是如果进程创建成功了,==0是表示正常结束。
- 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
- // 这个值在上面已经成功的得到了一个进程。
- Runtime.getRuntime().exec(new String[] { browser, url });
- }
- // 主方法 测试类
- }
- }
- public static void main(String[] args) {
- // 这里填写你的网址
- String url = "xxx";
- openURL(url);
- }
- }
方法二:
使用默认浏览器打开:
- 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);
- }
方法三:
通过获取环境变量的浏览器路径,然后启动浏览器
- 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 > |
转载请注明出处: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运行IE的方式来打开网址。
- String str = "cmd /c start iexplore http://blog.csdn.net/l1028386804";
- try {
- Runtime.getRuntime().exec(str);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 打开默认浏览器访问页面
- */
- public static void openDefaultBrowser(){
- //启用系统默认浏览器来打开网址。
- 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();
- }
- }
联系客服