Example usage for java.awt Desktop browse

List of usage examples for java.awt Desktop browse

Introduction

In this page you can find the example usage for java.awt Desktop browse.

Prototype

public void browse(URI uri) throws IOException 

Source Link

Document

Launches the default browser to display a URI .

Usage

From source file:com.ethercamp.harmony.desktop.HarmonyDesktop.java

private static void openBrowser(String url) {
    Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
    if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
        try {// w  ww.  j  av  a 2  s .  c om
            desktop.browse(URI.create(url));
        } catch (Exception e) {
            log.error("Problem opening browser", e);
        }
    }
}

From source file:org.yccheok.jstock.gui.CompanyNews.java

static void openWebpage(URI uri) {
    Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
    if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
        try {//w ww  .  java2  s.  com
            desktop.browse(uri);
        } catch (Exception e) {
            log.error(null, e);
        }
    }
}

From source file:com.google.oacurl.Login.java

private static void launchBrowser(LoginOptions options, String authorizationUrl) {
    logger.log(Level.INFO, "Redirecting to URL: " + authorizationUrl);

    boolean browsed = false;
    if (options.getBrowser() == null) {
        if (Desktop.isDesktopSupported()) {
            Desktop desktop = Desktop.getDesktop();
            if (desktop.isSupported(Action.BROWSE)) {
                try {
                    desktop.browse(URI.create(authorizationUrl));
                    browsed = true;/*  w  ww  . ja  v  a 2  s . c om*/
                } catch (IOException e) {
                    // In some situations "BROWSE" appears supported but throws an
                    // exception.
                    logger.log(Level.WARNING, "Error opening browser for Desktop#browse(String)",
                            options.isVerbose() ? e : null);
                }
            } else {
                logger.log(Level.WARNING, "java.awt.Desktop BROWSE action not supported.");
            }
        } else {
            logger.log(Level.WARNING, "java.awt.Desktop not supported. You should use Java 1.6.");
        }
    }

    if (!browsed) {
        String browser = options.getBrowser();
        if (browser == null) {
            browser = "google-chrome";
        }

        try {
            Runtime.getRuntime().exec(new String[] { browser, authorizationUrl });
        } catch (IOException e) {
            logger.log(Level.SEVERE,
                    "Error running browser: " + browser + ". "
                            + "Specify a browser with --browser or use --nobrowser to print URL.",
                    options.isVerbose() ? e : null);
            System.exit(-1);
        }
    }
}

From source file:org.openstreetmap.josm.plugins.mapillary.utils.MapillaryUtils.java

/**
 * Open the default browser in the given URL.
 *
 * @param url The (not-null) URL that is going to be opened.
 * @throws IOException when the URL could not be opened
 *///from  w w  w  .j  av a2  s .co m
public static void browse(URL url) throws IOException {
    if (url == null) {
        throw new IllegalArgumentException();
    }
    Desktop desktop = Desktop.getDesktop();
    if (desktop.isSupported(Desktop.Action.BROWSE)) {
        try {
            desktop.browse(url.toURI());
        } catch (URISyntaxException e1) {
            throw new IOException(e1);
        }
    } else {
        Runtime runtime = Runtime.getRuntime();
        runtime.exec("xdg-open " + url);
    }
}

From source file:com.igormaznitsa.sciareto.ui.UiUtils.java

private static void showURLExternal(@Nonnull final URL url) {
    if (Desktop.isDesktopSupported()) {
        final Desktop desktop = Desktop.getDesktop();
        if (desktop.isSupported(Desktop.Action.BROWSE)) {
            try {
                desktop.browse(url.toURI());
            } catch (Exception x) {
                LOGGER.error("Can't browse URL in Desktop", x);
            }//  www  . j  av a  2s. c  om
        } else if (SystemUtils.IS_OS_LINUX) {
            final Runtime runtime = Runtime.getRuntime();
            try {
                runtime.exec("xdg-open " + url);
            } catch (IOException e) {
                LOGGER.error("Can't browse URL under Linux", e);
            }
        } else if (SystemUtils.IS_OS_MAC) {
            final Runtime runtime = Runtime.getRuntime();
            try {
                runtime.exec("open " + url);
            } catch (IOException e) {
                LOGGER.error("Can't browse URL on MAC", e);
            }
        }
    }

}

From source file:org.duracloud.syncui.SyncUIDriver.java

private static void launchBrowser(final String url) {
    if (!java.awt.Desktop.isDesktopSupported()) {
        log.warn("Desktop is not supported. Unable to open");

    } else {/*from   ww  w.  j ava2  s . c o  m*/
        java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
        if (!desktop.isSupported(java.awt.Desktop.Action.BROWSE)) {
            log.warn("Desktop doesn't support the browse action.");
        } else {
            java.net.URI uri;
            try {
                uri = new java.net.URI(url);
                desktop.browse(uri);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
    }
}

From source file:tpp.TPPFrame.java

private static void browseHelp() {
    if (Desktop.isDesktopSupported()) {
        Desktop desktop = Desktop.getDesktop();
        try {//w  w w.jav a 2 s . co m
            desktop.browse(new URI(TargetedProjectionPursuit.HELP_URL));
        } catch (Exception e) {
            // TODO: error handling
        }
    } else {
        // TODO: error handling
    }
}

From source file:net.semanticmetadata.lire.utils.FileUtils.java

/**
 * Opens a browser windows th<t shows the given URI.
 *
 * @param uri the path to the file to show in the browser window.
 *///  w ww . ja  v  a 2 s .c om
public static void browseUri(String uri) {
    if (!java.awt.Desktop.isDesktopSupported()) {
        System.err.println("Desktop is not supported (fatal)");
        System.exit(1);
    }

    java.awt.Desktop desktop = java.awt.Desktop.getDesktop();

    if (!desktop.isSupported(java.awt.Desktop.Action.BROWSE)) {
        System.err.println("Desktop doesn't support the browse action (fatal)");
        System.exit(1);
    }

    try {
        java.net.URI url = new java.net.URI(uri);
        desktop.browse(url);
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
}

From source file:edu.umd.cs.submit.CommandLineSubmit.java

public static String[] getSubmitUserForOpenId(String courseKey, String projectNumber, String baseURL)
        throws UnsupportedEncodingException, URISyntaxException, IOException {
    Console console = System.console();

    boolean requested = false;
    String encodedProjectNumber = URLEncoder.encode(projectNumber, "UTF-8");
    URI u = new URI(baseURL + "/view/submitStatus.jsp?courseKey=" + courseKey + "&projectNumber="
            + encodedProjectNumber);/*  ww w . ja v a 2 s.c  o m*/

    if (java.awt.Desktop.isDesktopSupported()) {
        Desktop desktop = java.awt.Desktop.getDesktop();
        if (desktop.isSupported(Desktop.Action.BROWSE)) {
            System.out.println(
                    "Your browser will connect to the submit server, which may require you to authenticate yourself");
            System.out.println("Please do so, and then you will be shown a page with a textfield on it");
            System.out.println("Then copy that text and paste it into the prompt here");
            desktop.browse(u);
            requested = true;
        }
    }
    if (!requested) {
        System.out.println("Please enter the following URL into your browser");
        System.out.println("  " + u);
        System.out.println();
        System.out.println(
                "Your browser will connect to the submit server, which may require you to authenticate yourself");
        System.out.println("Please do so, and then you will be shown a page with a textfield on it");
        System.out.println("Then copy that text and paste it into the prompt here");

    }
    while (true) {
        System.out.println();
        System.out.println("Submission verification information from browser? ");
        String info = new String(console.readLine());
        if (info.length() > 2) {
            int checksum = Integer.parseInt(info.substring(info.length() - 1), 16);
            info = info.substring(0, info.length() - 1);
            int hash = info.hashCode() & 0x0f;
            if (checksum == hash) {
                String fields[] = info.split(";");
                if (fields.length == 2) {
                    return fields;

                }
            }
        }
        System.out.println("That doesn't seem right");
        System.out.println(
                "The information should be your account name and a string of hexidecimal digits, separated by a semicolon");
        System.out.println("Please try again");
        System.out.println();
    }
}

From source file:com.xmage.launcher.XMageLauncher.java

private static void openWebpage(String uri) {
    Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
    if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
        try {/*w ww  . j  av  a2s .  c om*/
            desktop.browse(new URI(uri));
        } catch (URISyntaxException ex) {
            logger.error("Error: ", ex);
        } catch (IOException ex) {
            logger.error("Error: ", ex);
        }
    }
}