32

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

fabian
  • 80,457
  • 12
  • 86
  • 114
Advanced
  • 1,457
  • 2
  • 13
  • 12

6 Answers6

46

Use hostServices.showDocument(location).

Try placing the following code in your application's start method:

getHostServices().showDocument("http://www.yahoo.com");
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • What about calling this method from another classes?I have buttons in different `fxml controllers classes ` which need to open the default browser in some websites , what can i do for it :) ? – GOXR3PLUS Jan 19 '17 at 04:46
  • For most cases you can add a static accessor to your [`Application`](https://docs.oracle.com/javase/8/javafx/api/javafx/application/Application.html) subclass which calls `getHostServices()`. If you want do not wish to use a static method and you want your FXML controllers to be used in multiple applications (e.g. they are library controls independent of any application), then you will need to pass the [application instance into the FXML controllers](http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml). – jewelsea Jan 19 '17 at 17:45
  • I get a `java.lang.Exception: No web browser found` :/ – xeruf Feb 01 '19 at 10:31
16

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)

ceklock
  • 6,143
  • 10
  • 56
  • 78
  • 10
    Note that classes such as HostServicesDelegate are not part of the publicly documented JavaFX API and may not be available in later Java versions (such as Java 9). – jewelsea Jan 19 '16 at 20:24
12

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.

Eldelshell
  • 6,683
  • 7
  • 44
  • 63
11

Try This:

try {
    Desktop.getDesktop().browse(new URL("https://google.com").toURI());
} catch (IOException e) {
    e.printStackTrace();
} catch (URISyntaxException e) {
    e.printStackTrace();
}
Aupr
  • 715
  • 9
  • 14
2

It cannot be done, seems, because this feature is not implemented : https://javafx-jira.kenai.com/browse/RT-210

The matter is that you will not be able to launch anything, what requires awt-stack and jfx in the same VM. The decision - is to use a separate JVM. Just launch a separate VM, and accept commands on browsing by socket.

That is one way, another way - is to find any other way of browser call from java - this is a task not specific to javafx-2, but to java at all.

But developer has added a comment :

Anthony Petrov added a comment - May, 17 2013 05:09 PM Note that FX8 allows headful AWT to run in the same VM with FX. So the AWT API should work.

Alexander Kirov
  • 3,624
  • 1
  • 19
  • 23
1

Here is a script that works inside the scene controller, when a button is activated:

package sample;


import com.sun.deploy.uitoolkit.impl.fx.HostServicesFactory;
import com.sun.javafx.application.HostServicesDelegate;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.stage.Stage;

public class Controller extends Application {

    public void openBrowser(ActionEvent actionEvent) throws Exception {

        HostServicesDelegate hostServices = HostServicesFactory.getInstance(this);
        getHostServices().showDocument("http://www.yahoo.com");

    }

    @Override
    public void start(Stage primaryStage) throws Exception {

    }
}
rainer
  • 3,295
  • 5
  • 34
  • 50