java - 仅在 chrome 浏览器中打开 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24239046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:01:23  来源:igfitidea点击:

java - open url in chrome browser only

javabrowserjavafxjavafx-2javafx-8

提问by naresh

I have an url, I am trying to open that url in web view of JavaFX but the content was not compatible with web view. It's only compatible to the chrome and firefox. So I would like to open that Url in chrome browser only instead of opening it in web view. is it possible?(I don't want to open it in default browser.)

我有一个 url,我试图在 JavaFX 的 web 视图中打开该 url,但内容与 web 视图不兼容。它仅与 chrome 和 firefox 兼容。所以我想只在 chrome 浏览器中打开那个 Url,而不是在 web 视图中打开它。可能吗?(我不想在默认浏览器中打开它。)

I tried the below code but we got the error as "Cannot run program "chrome": error=2, No such file or directory".

我尝试了下面的代码,但我们得到的错误是“无法运行程序“chrome”:错误=2,没有这样的文件或目录”。

Runtime.getRuntime().exec(new String[] { "Chrome", "http://goo.gl/EsomR0" });      

thanks,

谢谢,

回答by Nateowami

You'll need to use chrome.exeon Windows. On Linux this works for running Chromium:

您需要chrome.exe在 Windows上使用。在 Linux 上,这适用于运行 Chromium:

Runtime.getRuntime().exec(new String[] { "chromium-browser", "http://example.com/" });

Obviously it's not going to work for Chrome (or Windows), but we're talking about the principle. Another possibility is that Chrome may not be installed. Try running it from the command line. If it doesn't work there, it's not going to work in Java. Another problem could be that you capitalized "Chrome".

显然它不适用于 Chrome(或 Windows),但我们正在谈论原理。另一种可能性是可能未安装 Chrome。尝试从命令行运行它。如果它在那里不起作用,它就不会在 Java 中起作用。另一个问题可能是您将“Chrome”大写。

回答by cshu

Runtime.getRuntime().exec(new String[]{"cmd", "/c","start chrome http://goo.gl/EsomR0"});

This worked for me.

这对我有用。

-- Edit: Only worked on Windows

-- 编辑:仅适用于 Windows

回答by Darshana

I have tried following for android to open URL only in chrome and it worked for me:

我已经尝试在 android 中仅在 chrome 中打开 URL,它对我有用:

"googlechrome://navigate?url=" + url