Sep 8 2011

Launch Default Web Browser in Java

For the longest time I’ve used the BrowserLauncher library to open the default web browser to a specified web page from Java. BrowserLauncher is simple to use, just import edu.stanford.ejalbert.BrowserLauncher and call openURL method with the desired website URL.

Since Java 1.6, the JDK has introduced the java.awt.Desktop class to do the same so you don’t need an additional third party jar. The Desktop class has the ability to launch the desktop’s default email client and default web browser given a URI. Here is how you can launch the desktop’s default web browser in Java.

// Launch your default email client with ...
URI email = new URI("mailto:myemail@mydomain.com");
Desktop.getDesktop().mail(email);

// Launch your default web browser with ...
URI url = new URI("http://www.mydomain.com");
Desktop.getDesktop().browse(url);