Skip to content

Instantly share code, notes, and snippets.

@robobenklein
Created November 1, 2014 16:48
Show Gist options
  • Save robobenklein/31bc9dd2318d14dcd48a to your computer and use it in GitHub Desktop.
Save robobenklein/31bc9dd2318d14dcd48a to your computer and use it in GitHub Desktop.
Open a webpage in the default browser.
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