Created
May 28, 2014 14:47
-
-
Save ALRubinger/8c3cc2285b1c5febfdfa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Open a browser at the given URL using {@link Desktop} if available, or alternatively output the | |
* URL to {@link System#out} for command-line applications. | |
* | |
* @param url URL to browse | |
*/ | |
public static void browse(String url) { | |
Preconditions.checkNotNull(url); | |
// Ask user to open in their browser using copy-paste | |
System.out.println("Please open the following address in your browser:"); | |
System.out.println(" " + url); | |
// Attempt to open it in the browser | |
try { | |
if (Desktop.isDesktopSupported()) { | |
Desktop desktop = Desktop.getDesktop(); | |
if (desktop.isSupported(Action.BROWSE)) { | |
System.out.println("Attempting to open that address in the default browser now..."); | |
desktop.browse(URI.create(url)); | |
} | |
} | |
} catch (IOException e) { | |
LOGGER.log(Level.WARNING, "Unable to open browser", e); | |
} catch (InternalError e) { | |
// A bug in a JRE can cause Desktop.isDesktopSupported() to throw an | |
// InternalError rather than returning false. The error reads, | |
// "Can't connect to X11 window server using ':0.0' as the value of the | |
// DISPLAY variable." The exact error message may vary slightly. | |
LOGGER.log(Level.WARNING, "Unable to open browser", e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment