Search code examples
javajavafxwebkitjavafx-webengine

JavaFX's WebEngine gets stuck in RUNNING state


I'm trying to use WebEngine as a headless browser. Here is a proof of concept, which closely mimics the example in the official Java documentation.

import javafx.scene.web.*;
import javafx.application.*;
import javafx.stage.*;
import javafx.concurrent.Worker.*;
import javafx.beans.value.*;

public class WebEngineTest extends Application {
    public void start(Stage s) {
        WebEngine we = new WebEngine();

        //Print state changes as they happen;
        //Should go READY -> SCHEDULED -> RUNNING -> SUCCEEDED
        we.getLoadWorker().stateProperty().addListener(
                new ChangeListener<State>() {
                    public void changed(ObservableValue ov, State oldState, State newState) {
                        System.out.println(oldState + " -> " + newState);
                    }
                });

        //Hit some website
        we.load("http://javafx.com");
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Output:

READY -> SCHEDULED
SCHEDULED -> RUNNING

SUCCEEDED never gets hit, nor any error condition for that matter. (Expected behaviour: the task should eventually terminate, with either SUCCEEDED state, or some error condition.)


Solution

  • Had same problem, link you have in comments helped me. Just declare your web engine outside of start method, otherwise GC takes care of it.