不使用 java.awt.Desktop API,打开默认浏览器访问指定链接的方法 (转)

本文介绍如何在Java中不使用Desktop API,跨平台调用默认浏览器打开链接。针对Windows和MacOSX,通过Runtime.exec调用本地命令或使用反射调用Apple专有方法。适用于未安装最新JRE的环境。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不使用 java.awt.Desktop API,打开默认浏览器访问指定链接的方法
在 Java SE 6 中提供了一套桌面 API 实现与本机桌面 API 的无缝集成,这些桌面 API 使用你的主机操作系统的文件关联以启动与特定文件类型相关联的应用程序。但是因为目前许多客户端并没有安装、部署 JRE 6.0 之后的 Java 运行环境,那么在旧的运行环境中怎样才能调用默认浏览器打开指定的链接呢?

这就是本文的主要内容。

我们把目标平台暂定为 Windows XP 和 Mac OS X。

Windows 平台

可以在控制台中使用 rundll32.exe 调用 url.dll 这个动态连接库打开浏览器访问指定的链接。那么,我们在 Java 程序中就可以使用 Runtime.exec 方法来调用这个命令。(关于 Runtime.exec 可以参考我 blog 中的另一篇文章: Java 程序调用 exe

None.gif String cmd = " rundll32 url.dll,FileProtocolHandler http://www.apple.com " ;
None.gifRuntime.getRuntime().exec(cmd);

当然,以上程序执行前,首先得判断当前的操作系统平台是否是 Windows。

None.gif private static final String WIN_ID = " Windows " ;
None.gif
ExpandedBlockStart.gifContractedBlock.gif public static boolean isWindowsPlatform() ... {
InBlock.gif String os = System.getProperty("os.name");
InBlock.gif
InBlock.gifif ( os != null && os.startsWith(WIN_ID))
InBlock.gifreturn true;
InBlock.gifelse
InBlock.gifreturn false;
ExpandedBlockEnd.gif }


Mac OS X

在 Mac 里稍微有些复杂。同样,我们需要判断当前操作系统平台是否是 Mac OS X。

None.gif private static final String MAC_ID = " Mac " ;
None.gif
ExpandedBlockStart.gifContractedBlock.gif public static boolean isMacPlatform() ... {
InBlock.gif String os = System.getProperty("os.name");
InBlock.gif
InBlock.gifif ( os != null && os.startsWith(MAC_ID))
InBlock.gifreturn true;
InBlock.gifelse
InBlock.gifreturn false;
ExpandedBlockEnd.gif }

Apple 公司实现的 JDK 里有这么一个类:com.apple.mrj.MRJFileUtils,在 Mac 下可以使用它提供的方法 openURL 打开浏览器访问链接。为了使我们的程序兼容标准 Java 运行环境,所以使用反射技术来使用这个方法。

ExpandedBlockStart.gif ContractedBlock.gif public static void openMacURL(String url) ... {
ExpandedSubBlockStart.gifContractedSubBlock.giftry...{
InBlock.gif Class MRJFileUtils = Class.forName("com.apple.mrj.MRJFileUtils");
ExpandedSubBlockStart.gifContractedSubBlock.gif Method openMethod = MRJFileUtils.getDeclaredMethod("openURL", new Class[] ...{String.class});
ExpandedSubBlockStart.gifContractedSubBlock.gif openMethod.invoke(MRJFileUtils,new Object[]...{formatString(url)});
ExpandedSubBlockStart.gifContractedSubBlock.gif }
catch(Exception e) ...{
InBlock.gif e.printStackTrace();
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif }

formatString( String ) 顾名思义是用来重新格式化目标 URL。

ExpandedBlockStart.gif ContractedBlock.gif public static String formatString(String str) ... {
InBlock.gif String retString="";
InBlock.gif String protocol = "";
InBlock.gif String host = "";
InBlock.gif String path = "";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.giftry ...{
InBlock.gif java.net.URL url = new java.net.URL(str);
InBlock.gif protocol = url.getProtocol();
InBlock.gif host = url.getHost();
InBlock.gif path = url.getPath();
ExpandedSubBlockStart.gifContractedSubBlock.gif }
catch (MalformedURLException ex) ...{
InBlock.gif path = str;
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.giffor(int i = 0; i < path.length(); i++) ...{
ExpandedSubBlockStart.gifContractedSubBlock.gifif(path.charAt(i) == ' ') ...{
InBlock.gif retString += "%20";
ExpandedSubBlockStart.gifContractedSubBlock.gif }
else if(path.charAt(i) == '.') ...{
InBlock.gif retString += "%2E";
ExpandedSubBlockStart.gifContractedSubBlock.gif }
else ...{
InBlock.gif retString += path.substring(i, i + 1);
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifif (!protocol.equals("")) ...{
InBlock.gif retString = protocol + "://" + host + retString;
ExpandedSubBlockStart.gifContractedSubBlock.gif }
else ...{
InBlock.gif retString = host + retString;
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gifreturn retString ;
ExpandedBlockEnd.gif }


Mac 下默认调用的浏览器是 Safari

o_goToURL.jpg

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10294527/viewspace-122995/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10294527/viewspace-122995/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值