admin管理员组文章数量:1025328
package com.leon.test;
import java.lang.reflect.Method;
// 实现打开浏览器并跳到指定网址的类
public class BareBonesBrowserLaunch {
/**
* 方法描述:打开浏览器
*
* @author leon 2016年10月28日 下午4:37:35
* @param url 打开的url
* @throws Exception
*/
public static void poenBrowser(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 = "http://www.baidu";
try {
poenBrowser(url);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
阿里云低价服务器1折特惠,优惠爽翻天,点我立即低价购买
package com.leon.test;
import java.lang.reflect.Method;
// 实现打开浏览器并跳到指定网址的类
public class BareBonesBrowserLaunch {
/**
* 方法描述:打开浏览器
*
* @author leon 2016年10月28日 下午4:37:35
* @param url 打开的url
* @throws Exception
*/
public static void poenBrowser(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 = "http://www.baidu";
try {
poenBrowser(url);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
阿里云低价服务器1折特惠,优惠爽翻天,点我立即低价购买
版权声明:本文标题:java实现打开浏览器并跳到指定网址的类 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/jiaocheng/1737922882a1521584.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论