Open URL in Java using Default Browser

An example of browsing a web URL in java using the default web browser.


Example


import java.awt.Desktop;
import java.net.URI;
class BrowseURL
{
public static void main(String args[]) throws Exception
{

// Create Desktop object
Desktop d=Desktop.getDesktop();

// Browse a URL, say google.com
d.browse(new URI("http://google.com"));

}
}

Try it yourself! It works! The default handler for http:// protocol will be given the address to open.

No comments: