How do I launch user-default web browser?

The code below show you how to browse a website using the user’s default web browser. To get the default web browser you can use the Desktop class browse(URI uri) method call.

package org.kodejava.example.awt;

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

public class RunningDefaultBrowser {
    public static void main(String[] args) {
        URI uri = URI.create("https://kodejava.org");
        try {
            // Get Desktop instance of the current browser context. If the platform 
            // doesn't support Desktop API an UnsupportedOperationException will be 
            // thrown.
            Desktop desktop = Desktop.getDesktop();

            // Browse the uri using user's default web browser.
            desktop.browse(uri);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Wayan

Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. Support me by donating.

Leave a Reply