Linux:在默认浏览器中打开 URL 的命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5116473/
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
Linux: command to open URL in default browser
提问by Mot
What command we have to execute (from Java, but that should not matter) on Linux (different common distributions) to open a given URL in the default browser?
我们必须在 Linux(不同的常见发行版)上执行什么命令(来自 Java,但这应该无关紧要)才能在默认浏览器中打开给定的 URL?
采纳答案by shellholic
The most cross-distribution one is xdg-open http://stackoverflow.com
最交叉分布的一种是 xdg-open http://stackoverflow.com
回答by krtek
At least on Debian and all its derivatives, there is a 'sensible-browser' shell script which choose the browser best suited for the given url.
至少在 Debian 及其所有衍生产品上,有一个“明智的浏览器”shell 脚本,可以选择最适合给定 url 的浏览器。
回答by Daniel Winterstein
In Java (version 6+), you can also do:
在 Java(版本 6+)中,您还可以执行以下操作:
Desktop d = Desktop.getDesktop();
d.browse(uri);
Though this won't work on all Linuxes. At the time of writing, Gnome is supported, KDE isn't.
尽管这不适用于所有 Linux。在撰写本文时,支持 Gnome,不支持 KDE。
回答by Daniel Winterstein
on ubuntu you can try gnome-open.
在 ubuntu 上你可以试试 gnome-open。
$ gnome-open http://www.google.com
$ gnome-open http://www.google.com
回答by alexsb
回答by Roman A. Taycher
I think using xdg-open http://example.com
is probably the best choice.
我认为使用xdg-open http://example.com
可能是最好的选择。
In case they don't have it installed I suppose they might have just kde-open
or gnome-open
(both of which take a single file/url) or some other workaround such as looping over common browser executable names until you find one which can be executed(using which). If you want a full list of workarounds/fallbacks I suggest reading xdg-open(it's a shell script which calls out to kde-open/gnome-open/etc. or some other fallback).
如果他们没有安装它,我想他们可能只有kde-open
或gnome-open
(两者都采用单个文件/url)或其他一些解决方法,例如循环浏览常见的浏览器可执行文件名称,直到找到可以执行的名称(使用 which )。如果你想要一个完整的变通方法/回退列表,我建议阅读 xdg-open(它是一个调用 kde-open/gnome-open/etc. 或其他一些回退的 shell 脚本)。
But since xdg-open and xdg-mime(used for one of the fallbacks,) are shell scripts I'd recommend including them in your application and if calling which xdg-open
fails add them to temporary PATH variable in your subprograms environment and call out to them. If xdg-open fails, I'd recommend throwing an Exception with an error message from what it output on stderr and catching the exception and printing/displaying the error message.
但是由于 xdg-open 和 xdg-mime(用于后备之一)是 shell 脚本,我建议将它们包含在您的应用程序中,如果调用which xdg-open
失败,请将它们添加到子程序环境中的临时 PATH 变量中并调用它们。如果 xdg-open 失败,我建议从它在 stderr 上输出的内容中抛出一个带有错误消息的异常,并捕获异常并打印/显示错误消息。
I would ignore the java awt Desktop solution as the bug seems to indicate they don't plan on supporting non-gnome desktops anytime soon.
我会忽略 java awt 桌面解决方案,因为该错误似乎表明他们不打算在短期内支持非 gnome 桌面。
回答by JimmyLandStudios
###1 Desktop's -or- Console use:
sensible-browser $URL; # Opinion: best. Target preferred APP.
# My-Server translates to: w3m [options] [URL or filename]
## [ -z "$BROWSER" ] && echo "Empty"
# Then, Set the BROWSER environment variable to your desired browser.
###2 Alternative
# Desktop (if [command-not-found] out-Dated)
x-www-browser http://tv.jimmylandstudios.xyz # firefox
###3 !- A Must Know -!
# Desktop (/usr/share/applications/*.desktop)
xdg-open $URI # opens about anything on Linux (w/ .desktop file)
回答by Kevin Grant
I believe the simplest method would be to use Python:
我相信最简单的方法是使用 Python:
python -m webbrowser "http://www.example.com/"
回答by Anand Varma
On distributions that come with the open command,
在带有 open 命令的发行版上,
$ open http://www.google.com
回答by Kshitiz Sharma
For opening a URL in the browser through the terminal, CentOS 7 users can use gio open command. For example, if you want to open google.com then gio open https://www.google.com
will open google.com URL in the browser.
对于通过终端在浏览器中打开 URL,CentOS 7 用户可以使用 gio open 命令。例如,如果您想打开 google.com,则将gio open https://www.google.com
在浏览器中打开 google.com URL。
xdg-open https://www.google.com
will also work but this tool has been deprecated, Use gio open
instead. I prefer this as this is the easiest way to open a URL using a command from the terminal.
xdg-open https://www.google.com
也可以使用,但此工具已被弃用,请gio open
改用。我更喜欢这个,因为这是使用终端命令打开 URL 的最简单方法。