Opens a page in a Web Browser - Java Native OS

Java examples for Native OS:Browser

Description

Opens a page in a Web Browser

Demo Code


//package com.java2s;
import java.awt.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class Main {
    /**/*w ww.  ja  v  a 2s. c om*/
     * Opens a page in a Web Browser
     *
     * @param uri The URI to open in a Browser
     */
    public static void openWebpage(URI uri) {
        Desktop desktop = Desktop.isDesktopSupported() ? Desktop
                .getDesktop() : null;
        if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
            try {
                desktop.browse(uri);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * Opens a page in a Web Browser
     *
     * @param url The URL to open in a Browser
     */
    public static void openWebpage(URL url) {
        try {
            openWebpage(url.toURI());
        } catch (URISyntaxException ex) {
            ex.printStackTrace();
        }
    }
}

Related Tutorials