启动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;
}