JAVA启动IE浏览器

启动ie浏览器

import java.awt.Desktop;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.URI;

/**
 * 启动系统IE浏览器
 * 
 * @author 罗勇
 * 
 * @date 2014-3-25
 */
public class Main {

	public static void main(String[] args) {
		openURL("http://www.baidu.com");
	}

	/**
	 * 调用系统默认浏览器打开url链接
	 */
	private static void openURL(String url) {
		String os = System.getProperty("os.name", "");
		if (os.startsWith("Windows")) {
			// Windows
			try {
				String SystemRoot = System.getenv("SystemRoot");// 获取系统盘路径
				String program = SystemRoot.substring(0, SystemRoot.indexOf(':'))
						+ ":\\Program Files\\Internet Explorer\\iexplore.exe";
				ProcessBuilder builder = new ProcessBuilder(program, url);
				builder.start();
			} catch (IOException e) {
				if (!openUrlByRuntime(url)) {
					openUrlByDesktop(url);
				}
			}
		}
	}

	private static boolean openUrlByRuntime(String url) {
		boolean res = true;
		LineNumberReader lr = null;
		try {
			// String[] cmd = { "rundll32", "url.dll,FileProtocolHandler", url};
			String[] cmd = { "cmd", "/c", "start", "iexplore", url };
			Process ps = Runtime.getRuntime().exec(cmd);
			InputStreamReader ir = new InputStreamReader(ps.getErrorStream());// 遇到错误返回
			lr = new LineNumberReader(ir);
			String line = null;
			while ((line = lr.readLine()) != null) {
				System.out.println(line);
				return false;
			}
		} catch (IOException e) {
			res = false;
		} finally {
			try {
				lr.close();
			} catch (Exception e2) {
			}
		}
		return res;
	}

	private static boolean openUrlByDesktop(String url) {
		boolean res = false;
		// 判断当前系统是否支持Java AWT Desktop扩展
		if (Desktop.isDesktopSupported()) {
			try {
				// 获取当前系统桌面扩展
				Desktop dp = Desktop.getDesktop();
				// 判断系统桌面是否支持要执行的功能
				if (dp.isSupported(Desktop.Action.BROWSE)) {
					// 获取系统默认浏览器打开链接
					// 创建一个URI实例
					URI uri = URI.create(url);
					dp.browse(uri);
					res = true;
				}
			} catch (NullPointerException e) {
				// 此为uri为空时抛出异常
			} catch (IOException e) {
				// 此为无法获取系统默认浏览器
			}
		}
		return res;
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值