How can I open the default system browser from a java fx application?
I'm trying to open a web url in the default system browser from javafx. I didn't find any official documentation regard this. Any clue?
EDIT: I've found a tutorial but it doesn't work. I'm using MacOsX and I tried launching
java.awt.Desktop.getDesktop().browse(new URI(url));
but I get an HeadlessExcelption
Use hostServices.showDocument(location).
Try placing the following code in your application's start method:
getHostServices().showDocument("http://www.yahoo.com");
Complementing jewelsea's answer, if you don't know how to call getHostServices() then try this at your main class:
HostServicesDelegate hostServices = HostServicesFactory.getInstance(this);
hostServices.showDocument(WEBSITE);
http://docs.oracle.com/javafx/2/api/javafx/application/HostServices.html#showDocument(java.lang.String)
Another option is to use ProcessBuilder
:
public static void openWebpage(String url) {
try {
new ProcessBuilder("x-www-browser", url).start();
} catch (IOException e) {
e.printStackTrace();
}
}
You can use this option if Desktop.getDesktop().browse(uri)
for some reason hangs without any error.