简体   繁体   English

在不同的浏览器java swing中打开一个链接

[英]open a link in different browser java swing

How do i open a link in different browser in java swing?? 如何在java swing中打开不同浏览器中的链接?

I've read this article : http://www.roseindia.net/tutorial/java/swing/openBrowser.html 我读过这篇文章: http//www.roseindia.net/tutorial/java/swing/openBrowser.html

This is opening in my default browser. 这是在我的默认浏览器中打开的。 But when I want to open in a different browser 但是当我想在不同的浏览器中打开时

it gives an error message: "Cannot run program "which": CreateProcess error=2, The system cannot find the file specified " 它给出了一条错误消息:“无法运行程序”其中“:CreateProcess error = 2,系统找不到指定的文件”

Generally speaking, to open a link in the user's default browser you should really be using the more modern approach: 一般来说,要在用户的默认浏览器中打开链接,您应该使用更现代的方法:

String url = "www.stackoverflow.com";
Desktop desktop = java.awt.Desktop.getDesktop();
desktop.browse(url);

No need to mess about with working out what OS you are running on (as your linked example attempts to do). 无需弄乱你正在运行的操作系统 (正如您的链接示例所尝试的那样)。 Far better to let java.awt.Desktop take care of finding an appropriate browser or application to open the URL (see the documentation for more details). java.awt.Desktop负责找到合适的浏览器或应用程序以打开URL(请参阅文档以获取更多详细信息)。

There's also a part of the API that gracefully handles permissions and 'unusual' OS setups. 还有一部分API可以优雅地处理权限和“不寻常”的操作系统设置。 If there is the possibility that your code will run under a restricted security policy or on a platform that may not have a browser, then you can check up front rather than wait for the exception from the call to browse . 如果您的代码可能在受限制的安全策略下运行,或者在可能没有浏览器的平台上运行,那么您可以预先检查而不是等待调用中的异常进行browse

// check if java.awt.Desktop is available on the current platform
java.awt.Desktop.isDesktopSupported();

// check the current platform and security policy will let you browse to a url
Desktop desktop = java.awt.Desktop.getDesktop();
desktop.isSupported(Desktop.Action.BROWSE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM