I found a way to open a link on default browser using HostServices.
getHostServices().showDocument("http://www.google.com");
Generally speaking, you can use Desktop#open(file)
to open a file natively as next:
final Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.OPEN)) {
desktop.open(file);
} else {
throw new UnsupportedOperationException("Open action not supported");
}
Launches the associated application to open the file. If the specified file is a directory, the file manager of the current platform is launched to open it.
More specifically, in case of a browser you can use directly Desktop#browse(uri)
, as next:
final Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
desktop.browse(uri);
} else {
throw new UnsupportedOperationException("Browse action not supported");
}
Launches the default browser to display a
URI
. If the default browser is not able to handle the specifiedURI
, the application registered for handlingURIs
of the specified type is invoked. The application is determined from the protocol and path of theURI
, as defined by theURI
class. If the calling thread does not have the necessary permissions, and this is invoked from within an applet,AppletContext.showDocument()
is used. Similarly, if the calling does not have the necessary permissions, and this is invoked from within a Java Web Started application,BasicService.showDocument()
is used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With