browse() - Launch Default Web Browser

This section provides a tutorial example on how to use Desktop.browe() method to launch the default Web browser on the desktop of the operating system.

If you want to launch the default Web browser on the current desktop of the operating system, you can get the default Desktop object and call its browse() method. Here is a sample program:

/* DesktopWebBrowser.java
 * Copyright (c) 1997-2024 HerongYang.com. All Rights Reserved.
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.net.URI;
public class DesktopWebBrowser {
   public static void main(String[] a) {
      try {
         URI uri = new URI("https://www.herongyang.com/Swing");
         Desktop desktop = Desktop.getDesktop();
         if (desktop != null) desktop.browse(uri);
      } catch (Exception e) {
        e.printStackTrace();
      }
   }
}

If you run this program, you will see the default Web browser is started with the given Website displayed. On my Windows 10 computer, I am getting the Edge browser launched.

Launch Web Browser on Desktop
Launch Web Browser on Desktop

On my macOS computer, I am getting the Safari browser launched.

Table of Contents

 About This Book

 JDK (Java Development Kit)

 Introduction of Java Swing Package

 Graphics Environment of the Local System

 JFrame - Main Frame Class

 JLabel - Swing Label Class

 JButton - Swing Button Class

 JCheckBox - Swing Check Box Class

 JRadioButton - Swing Radio Button Class

 JTextField - Swing Text Field Class

 JComboBox - Swing Combo Box Class

 Menu Bar, Menus, Menu Items and Listeners

 Creating Internal Frames inside the Main Frame

 Layout of Components in a Container

 LookAndFeel and UIManager

 Option Dialog Boxes

 JEditorPane - The Editor Pane Class

 SwingWorker - The Background Task Worker

 AWT (Abstract Windows Toolkit)

Integration with Desktop System

 javax.awt.Desktop and Related Methods

browse() - Launch Default Web Browser

 mail() - Launch Default Email Program

 edit() - Launch Default Editor for Given File

 Archived Tutorials

 References

 Full Version in PDF/EPUB