打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
java程序中指定某个浏览器打开网页的实现方法

本文主要介绍的是利用java程序打开指定某个的浏览器

方法一:

  1. import java.lang.reflect.Method;
  2. //实现打开浏览器并跳到指定网址的类
  3. public class TestUrl {
  4. public static void openURL(String url) {
  5. try {
  6. browse(url);
  7. } catch (Exception e) {
  8. }
  9. }
  10. private static void browse(String url) throws Exception {
  11. // 获取操作系统的名字
  12. String osName = System.getProperty("os.name", "");
  13. if (osName.startsWith("Mac OS")) {
  14. // 苹果的打开方式
  15. Class fileMgr = Class.forName("com.apple.eio.FileManager");
  16. Method openURL = fileMgr.getDeclaredMethod("openURL",new Class[] { String.class });
  17. openURL.invoke(null, new Object[] { url });
  18. } else if (osName.startsWith("Windows")) {
  19. // windows的打开方式。
  20. Runtime.getRuntime().exec(
  21. "rundll32 url.dll,FileProtocolHandler " + url);
  22. } else {
  23. // Unix or Linux的打开方式
  24. String[] browsers = { "firefox", "opera", "konqueror", "epiphany",
  25. "mozilla", "netscape" };
  26. String browser = null;
  27. for (int count = 0; count < browsers.length && browser == null; count++) {
  28. // 执行代码,在brower有值后跳出,
  29. // 这里是如果进程创建成功了,==0是表示正常结束。
  30. if (Runtime.getRuntime()
  31. .exec(new String[] { "which", browsers[count] })
  32. .waitFor() == 0)
  33. browser = browsers[count];
  34. if (browser == null)
  35. throw new Exception("Could not find web browser");
  36. else
  37. // 这个值在上面已经成功的得到了一个进程。
  38. Runtime.getRuntime().exec(new String[] { browser, url });
  39. }
  40. // 主方法 测试类
  41. }
  42. }
  43. public static void main(String[] args) {
  44. // 这里填写你的网址
  45. String url = "xxx";
  46. openURL(url);
  47. }
  48. }

方法二:

使用默认浏览器打开:

  1. String site = "www.baidu.com";
  2. try {
  3. Desktop desktop = Desktop.getDesktop();
  4. if (desktop.isDesktopSupported()
  5. && desktop.isSupported(Desktop.Action.BROWSE)) {
  6. URI uri = new URI(site);
  7. desktop.browse(uri);
  8. }
  9. } catch (IOException ex) {
  10. System.out.println(ex);
  11. } catch (URISyntaxException ex) {
  12. System.out.println(ex);
  13. }

方法三:

通过获取环境变量的浏览器路径,然后启动浏览器

  1. String firefox = "C:\\Program Files\\Mozilla Firefox\\firefox.exe";
  2. Map map = System.getenv();
  3. for (Iterator itr = map.keySet().iterator(); itr.hasNext();) {
  4. String value = (String) map.get((String) itr.next());
  5. if (value.contains("firefox.exe")) {
  6. firefox = value;
  7. break;
  8. }
  9. }
  10. Runtime.getRuntime().exec(new String[] { firefox, "www.baidu.com" });

方法四:

js方式:

<scripttype="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

  1. package com.lyz.test;
  2. import java.awt.Desktop;
  3. import java.io.IOException;
  4. import java.net.URI;
  5. import java.net.URISyntaxException;
  6. /**
  7. * @author liuyazhuang
  8. * @time 2015-10-22
  9. *
  10. */
  11. public class Gotourl {
  12. /**
  13. * 打开IE浏览器访问页面
  14. */
  15. public static void openIEBrowser(){
  16. //启用cmd运行IE的方式来打开网址。
  17. String str = "cmd /c start iexplore http://blog.csdn.net/l1028386804";
  18. try {
  19. Runtime.getRuntime().exec(str);
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. /**
  25. * 打开默认浏览器访问页面
  26. */
  27. public static void openDefaultBrowser(){
  28. //启用系统默认浏览器来打开网址。
  29. try {
  30. URI uri = new URI("http://blog.csdn.net/l1028386804");
  31. Desktop.getDesktop().browse(uri);
  32. } catch (URISyntaxException e) {
  33. e.printStackTrace();
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. public static void main(String[] args) {
  39. openIEBrowser();
  40. openDefaultBrowser();
  41. }
  42. }


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
JAVA_OPTS参数-Xms和-Xmx的作用
Java Process中waitFor()的问题
如何利用Java获取进程的信息(通过tasklist和cmd与Windows进行交互)
Runtime.getRuntime().exec()常见问题
Spring AOP实现后台管理系统日志管理
java的关闭钩子(Shutdown Hook)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服