当我在我的开发环境(Eclipse)中启动我的应用程序时,它会运行,但是,当我尝试将它导出到可运行的 .jar 文件时,它给了我以下错误:
Exception in thread "main" java.lang.ExceptionInInitializerError
at mallApp.MainTwo.<clinit>(MainTwo.java:24)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:398)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:59)
Caused by: java.lang.IllegalStateException: Toolkit not initialized
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:410)
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:405)
at com.sun.javafx.application.PlatformImpl.setPlatformUserAgentStylesheet(PlatformImpl.java:695)
at com.sun.javafx.application.PlatformImpl.setDefaultPlatformUserAgentStylesheet(PlatformImpl.java:657)
at javafx.scene.control.Control.<clinit>(Control.java:99)
... 4 more
问题是“Caused by: java.lang.IllegalStateException: Toolkit not initialized”。
这里有许多关于类似问题的线索,但似乎没有一个完全相同,而且我尝试过的修复对我不起作用。我的类扩展了 Application,因此在 main 方法中有 Application.launch(args)。这应该会初始化 Toolkit,但在导出到 .jar 时它不会出于某种原因。
当我尝试以不同的方式添加 Toolkit 时,例如使用 JFXPanel 或Platform.startup(Runnable)
,它给了我相反的错误,说 Toolkit 已经初始化。
public class MainTwo extends Application {
...
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage arg0) throws Exception {
model = new Main2Model();
c = model.getCompanyList();
primaryStage = arg0;
primaryStage.setTitle("MallApp");
primaryStage.centerOnScreen();
showMainView();
}
问题是我在声明它们时已经初始化了静态 javafx-attributes。当我将字段的初始化移动到 start-method 时,问题得到了解决,JAR 变得可运行,如下所示:
而不是:
private static TextField total = new TextField();
private static ObservableList<String> items = FXCollections.observableArrayList();
public void start(Stage arg0) throws Exception {
model = new Main2Model();
c = model.getCompanyList();
primaryStage = arg0;
primaryStage.setTitle("MallApp");
primaryStage.centerOnScreen();
showMainView();
}
我有:
private static TextField total;
private static ObservableList<String> items;
@Override
public void start(Stage arg0) throws Exception {
total = new TextField();
items = FXCollections.observableArrayList();
model = new Main2Model();
c = model.getCompanyList();
primaryStage = arg0;
primaryStage.setTitle("MallApp");
primaryStage.centerOnScreen();
showMainView();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句