Created
November 1, 2014 16:48
-
-
Save robobenklein/31bc9dd2318d14dcd48a to your computer and use it in GitHub Desktop.
Open a webpage in the default browser.
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
import java.awt.Desktop; | |
import java.net.URI; | |
import java.net.URL; | |
import java.net.URISyntaxException; | |
import java.net.MalformedURLException; | |
public static void openWebpage(String url) { | |
try { | |
try { | |
openWebpage(new URL(url).toURI()); | |
} catch (MalformedURLException e) { | |
System.err.println("Malformed URL Exception. How silly."); | |
e.printStackTrace(); | |
} | |
} catch (URISyntaxException e) { | |
e.printStackTrace(); | |
} | |
} | |
// Thanks to 'Vulcan' from Stackoverflow | |
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 e) { | |
System.err.println("Some kind of exception occured, sorry I don't know more."); | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment