本文主要介绍的是利用java程序打开指定某个的浏览器
方法一:
import java.lang.reflect.Method; 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++) { // 这里是如果进程创建成功了,==0是表示正常结束。 .exec(new String[] { "which", browsers[count] }) browser = browsers[count]; throw new Exception("Could not find web browser"); 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) {
方法三:
通过获取环境变量的浏览器路径,然后启动浏览器
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");
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/49334975
import java.io.IOException; import java.net.URISyntaxException; public static void openIEBrowser(){ String str = "cmd /c start iexplore http://blog.csdn.net/l1028386804"; Runtime.getRuntime().exec(str); } catch (IOException e) { public static void openDefaultBrowser(){ 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) {
|