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?
11 Answers
The most cross-distribution one is xdg-open http://stackoverflow.com

- 5,974
- 1
- 20
- 30
-
8Unfortunately not present in RHEL – Ondra Žižka Mar 12 '11 at 14:48
-
@OndraŽižka sensible-browser is debian-only too, isn't it? What would work in RHEL? – cmc Nov 15 '13 at 13:52
-
2It exists in RHEL, install the package 'xdg-utils'. – Tim Peters Oct 07 '14 at 02:06
-
Not present in my Debian 7. Have to use 'wget' instead. – estornes Feb 04 '15 at 21:26
-
In Debian, it's package "xdg-utlls" too, just like in RHEL. – sleske Aug 01 '15 at 17:12
-
Why does it always prompt `ERROR:browser_gpu_channel_host_factory.cc(120) | Failed to launch GPU process.` – Abhishta Gatya Mar 31 '18 at 05:00
I believe the simplest method would be to use Python:
python -m webbrowser "http://www.example.com/"

- 5,363
- 1
- 21
- 24
-
7works like a charm. also cross platform (at least works on mac & linux) – Paul Liang Mar 09 '18 at 04:20
-
-
I like this solution better for a cross-platform use case, but for Linux only, it does take ~7-8x as much time to run on my system compared to `xdg-open` for the same url. – Taylor D. Edmiston Oct 03 '19 at 16:23
-
Just as a note for people running this on windows: I've found it often opens up Internet Explorer. (instead of the user's configured default) But it works, I guess. – Lazerbeak12345 Sep 24 '20 at 21:29
-
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.

- 2,418
- 1
- 29
- 41
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.

- 26,334
- 5
- 56
- 84
On distributions that come with the open command,
$ open http://www.google.com

- 150
- 1
- 2
###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)

- 447
- 6
- 4
-
4
-
When using any of these commands in a Shell Script you'll need to test if they exist first (e.g. command -v $CMD ). $? = 0 – JimmyLandStudios Feb 13 '20 at 17:03
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.

- 68,213
- 24
- 160
- 246

- 18,619
- 19
- 86
- 141
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.

- 53
- 1
- 6
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"

- 3,786
- 2
- 36
- 56