Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a URL in the default browser

Tags:

java

macos

I was wondering if there was a standard way to open URLs in the users default browser. I've been using the desktop.browse() way, but after a bit of testing I can't seem to make this work on OSX while it works fine on Windows. My attempts at research have brought up mostly old articles, so I thought there may be a more modern method I can't find. Ideally there would be one approach that could work for every system, but lacking that I can always make it branch off by system. So my questions are this:

  • Is there an approach that opens up the default browser on any system to a specific URL?
  • If not, then what are the best methods for doing this on OSX and Linux?
like image 776
SwR Avatar asked Jul 10 '13 21:07

SwR


1 Answers

java.awt.Desktop is the class you're looking for.

import java.awt.Desktop;
import java.net.URI;

// ...

if(Desktop.isDesktopSupported())
{
  Desktop.getDesktop().browse(new URI("http://www.example.com"));
}

That is what I would do. answer comes from this site at :: How to open the default webbrowser using java

like image 123
Shane Superfly MacNeill Avatar answered Sep 24 '22 01:09

Shane Superfly MacNeill