Linux: command to open URL in default browser

LinuxBrowserDefault

Linux Problem Overview


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 Solutions


Solution 1 - Linux

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

Solution 2 - Linux

I believe the simplest method would be to use Python:

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

Solution 3 - Linux

on ubuntu you can try gnome-open.

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

Solution 4 - Linux

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.

Solution 5 - Linux

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

Solution 6 - Linux

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

Solution 7 - Linux

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.

Solution 8 - Linux

On distributions that come with the open command,

$ open http://www.google.com

Solution 9 - Linux

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.

Solution 10 - Linux

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.

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.

Solution 11 - Linux

If you are in Windows10 (including WSL2 *nix shells) you can try:

  explorer.exe   https://stackoverflow.com

  or

  cmd.exe /c start   https://stackoverflow.com/?foo=bar

Weird but it works!

Note: In the case of WSL there is a known bug which prohibits passing query parameters into the url. The workaround is to use "cmd.exe /c start url"

https://github.com/microsoft/WSL/issues/3832

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMotView Question on Stackoverflow
Solution 1 - LinuxshellholicView Answer on Stackoverflow
Solution 2 - LinuxKevin GrantView Answer on Stackoverflow
Solution 3 - Linuxuser1203496View Answer on Stackoverflow
Solution 4 - LinuxDaniel WintersteinView Answer on Stackoverflow
Solution 5 - LinuxkrtekView Answer on Stackoverflow
Solution 6 - LinuxJimmyLandStudiosView Answer on Stackoverflow
Solution 7 - LinuxRoman A. TaycherView Answer on Stackoverflow
Solution 8 - LinuxAnand VarmaView Answer on Stackoverflow
Solution 9 - LinuxalexsbView Answer on Stackoverflow
Solution 10 - LinuxKshitiz SharmaView Answer on Stackoverflow
Solution 11 - LinuxXDSView Answer on Stackoverflow