how-to

Network

How to launch default web browser for web page browsing

For this example you need:

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

When your platform supports desktop operation, you can launch default web browser with specific (web) page. Here is the code:

/* -- is desktop supported on current platform ? */

if ( Desktop.isDesktopSupported() ) {

  /* -- get desktop instance */

  Desktop desktop = Desktop.getDesktop();

  /* -- is supported browse operation ? */

  if ( desktop.isSupported( Desktop.Action.BROWSE ) ) {

    /* -- try to show web page */

    try {
      desktop.browse( new URI( "http://www.javaexamplecenter.com" ) );
    } catch ( IOException ex ) {
    } catch ( URISyntaxException ex ) {
      Logger.getLogger(Window.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
}

When is all OK, default browser will be launched with web page.