我使用 JavaFX 构建的项目在编译器中运行良好,但是当我将其打包为 EXe 可执行文件时,它出错了。运行打包好的EXE程序时提示的错误信息如下:
java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2459)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at cn.ambit.app.App.start(App.java:40)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
发生错误的函数:
@Override
public void start(Stage initStage) throws Exception {
FXMLLoader initFXMLLoader = new FXMLLoader(App.class.getResource("/view/initializationStage.fxml"));
AnchorPane anchorPane = initFXMLLoader.load(); // line 40
Scene scene = new Scene(anchorPane);
JMetroThemeManager.setSceneStyle(scene);
initStage.setScene(scene);
initStage.setResizable(false);
initStage.titleProperty().bind(I18N.createStringBinding("title"));
initStage.show();
}
从错误提示来看,似乎是FXML文件读取失败,但奇怪的是我在其他项目中使用相同的方法读取FXML文件,打包成EXE可执行文件时效果很好,对此我感到困惑。