Linux: command to open URL in default browser

What command should we execute (from Java, but it doesn’t matter) on Linux (different common distributions) in order to open this URL in the default browser?

+63
linux browser default
25 Feb 2018-11-11T00:
source share
10 answers

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

+86
Feb 25 '11 at 11:16
source share

I believe the easiest method would be to use Python:

 python -m webbrowser "http://www.example.com/" 
+20
Apr 07 '16 at 13:04 on
source share

on ubuntu you can try gnome-open.

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

+10
Feb 15 '12 at 12:37
source share

In Java (version 6+) you can also:

 Desktop d = Desktop.getDesktop(); d.browse(uri); 

Although this will not work on all Linux. At the time of writing, Gnome is supported, KDE is not.

+8
Feb 15 '12 at 12:30
source share

At least Debian and all its derivatives have a “reasonable browser” shell script that selects the browser that is most suitable for the given URL.

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

+6
Feb 25 '11 at 11:12
source share

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

If they are not installed, I suggest that they can only have kde-open or gnome-open (both of which take the same file / url) or some other workaround, such as switching to normal browser executables, until you find one that can be performed (with which). If you need a complete list of workarounds / backups, I suggest reading xdg-open (this is a shell script that calls kde-open / gnome-open / etc or some other kind of backup).

But since xdg-open and xdg-mime (used for one of the backups) are shell scripts that I would recommend including in their application, and if calling which xdg-open does not allow adding them to the PATH temporary variable in your environment subroutines and will appeal to them. If xdg-open fails, I would recommend throwing an exception with the error message from what it printed on stderr, and catch the exception and print / display the error message.

I would ignore the java awt Desktop solution, as the error seems to indicate that they are not planning on supporting desktops without gnome in the near future.

+4
Apr 09 '14 at 12:03 on
source share
 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 
+2
Aug 01 '15 at 16:45
source share

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

+1
Apr 15 2018-12-14T00:
source share

On distributions that come with the open command,

 $ open http://www.google.com 
0
Aug 25 '16 at 1:06 on
source share

Good, really useful, Any way I started using wine for Windows software

https://www.feelingwisdom.net/2019/06/machine-learning-applications-and-its.html

0
Jun 05 '19 at 7:34
source share



All Articles