在服务器程序中(意味着没有安装 X)我真的只想加载一个包含一些 javascript 的 html 站点并获取渲染的 dom。我想以使用静态函数的方式做到这一点。我的问题是我无法让线程加入/完成。
package svg;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import kic.engine.util.DomUtils;
import javafx.embed.swing.JFXPanel;
public class Render extends Application {
private static final JFXPanel USELESS = new JFXPanel(); // do I really need this? just to avoid "Toolkit not initialized"
public static void main(String[] args) throws InterruptedException {
//launch(args);
compile();
System.out.println("stop");
}
public static void compile() throws InterruptedException {
Thread t = new Thread() {
@Override
public void run() {
new Render().start(new Stage()); //EDIT: copy/paste mistake
}
};
Platform.runLater(t);
t.join(); // never get here!
}
public void start(final Stage primaryStage) {
final WebView webView = new WebView();
final WebEngine engine = webView.getEngine();
//final WebEngine engine = new WebEngine();
//engine.documentProperty().addListener( new ChangeListener<Document>() { @Override public void changed(ObservableValue<? extends Document> prop, Document oldDoc, Document newDoc) {
engine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
public void changed(ObservableValue ov, Worker.State oldState, Worker.State newState) {
if (newState == Worker.State.SUCCEEDED) {
System.out.println(DomUtils.getDocumentString(engine.getDocument()));
primaryStage.close();
//Platform.setImplicitExit(true);
Platform.exit(); // platform will not exit ... it will exit if I use Application.launch(), but throws an error: java.lang.IllegalStateException: Attempt to call defer when toolkit not running
System.out.println("after exit");
}
}
});
engine.load("file:///myD3.html");
System.out.println("after load");
}
}
有人可以帮我获得一个静态函数,只需调用 webengine 并返回 dom。
编辑:即使是 Thread.currentThread().getThreadGroup().interrupt(); 不会阻止应用程序仅运行 System.exit(1),但我不想关闭 JVM。