JavaFX的WebEngine陷入运行状态
我正在尝试使用WebEngine作为无头浏览器。这是一个概念证明,它与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);
}
}
输出:
READY -> SCHEDULED
SCHEDULED -> RUNNING
成功永远不会被击中,也不会出现任何错误情况。(预期行为:任务最终应终止,状态为Successed或出现某种错误情况。)
# 1 楼答案
同样的问题,你在评论中的链接帮助了我。只需在start方法之外声明您的web引擎,否则GC就会处理它