Open URL using system default browser - Java Native OS

Java examples for Native OS:Browser

Description

Open URL using system default browser

Demo Code


//package com.java2s;
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class Main {
    public static void main(String[] argv) throws Exception {
        String url = "java2s.com";
        navigate(url);//from   w ww .j av  a  2s .c om
    }

    public static void navigate(String url) {
        Desktop desktop;
        if (Desktop.isDesktopSupported()) {
            desktop = Desktop.getDesktop();
            // Now enable buttons for actions that are supported.
            if (desktop.isSupported(Desktop.Action.BROWSE)) {
                URI uri = null;
                try {
                    //String dir = System.getProperty("user.dir");
                    uri = new URI(url);
                    desktop.browse(uri);
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                } catch (URISyntaxException use) {
                    use.printStackTrace();

                }
            }
        }
    }
}

Related Tutorials