Linux: command to open URL in default browser

浪尽此生 提交于 2019-11-27 05:12:01

问题


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?


回答1:


The most cross-distribution one is xdg-open http://stackoverflow.com




回答2:


I believe the simplest method would be to use Python:

python -m webbrowser "http://www.example.com/"



回答3:


on ubuntu you can try gnome-open.

$ gnome-open http://www.google.com




回答4:


In Java (version 6+), you can also do:

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.




回答5:


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.

http://man.he.net/man1/sensible-browser




回答6:


I think using xdg-open http://example.com is probably the best choice.

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).

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.

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.




回答7:


sensible-browser [options] [URL or filename]; # Said to be the best.
# Server translates to: 
echo $BROWSER;
w3m [options] [URL or filename]
# Desktop (X11): I use/prefer,
x-www-browser http://tv.jimmylandstudios.com



回答8:


I think a combination of xdg-open as described by shellholic and - if it fails - the solution to finding a browser using the which command as described here is probably the best solution.




回答9:


On distributions that come with the open command,

$ open http://www.google.com


来源:https://stackoverflow.com/questions/5116473/linux-command-to-open-url-in-default-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!