Open the given URL in the system web browser. - Java java.awt

Java examples for java.awt:Desktop

Description

Open the given URL in the system web browser.

Demo Code

/**/*  w  ww  .  j a v a 2 s.  c  o  m*/
 *
 * jerry - Common Java Functionality
 * Copyright (c) 2012-2015, Sandeep Gupta
 * 
 * http://sangupta.com/projects/jerry
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
//package com.java2s;
import java.awt.Desktop;
import java.awt.Desktop.Action;
import java.io.IOException;
import java.net.URI;

public class Main {
    /**
     * Open the given URL in the system web browser.
     * 
     * @param uri
     *            the {@link URI} to be opened
     * 
     * @return <code>true</code> if call to open was successfully made,
     *         <code>false</code> otherwise. A value of <code>true</code> DOES
     *         NOT guarantee that the {@link URI} was opened, but only that the
     *         call was successfully made.
     */
    public static boolean openURL(URI uri) {
        if (!Desktop.isDesktopSupported()) {
            return false;
        }

        Desktop d = Desktop.getDesktop();
        if (!d.isSupported(Action.BROWSE)) {
            return false;
        }

        try {
            d.browse(uri);
            return true;
        } catch (IOException e) {
            return false;
        }
    }
}

Related Tutorials