gpt4 book ai didi

java - 在 Eclipse 外部运行 jar 时未加载 FXML 布局

转载 作者:太空宇宙 更新时间:2023-11-04 06:29:28 25 4
gpt4 key购买 nike

我尝试按如下方式加载我的 fxml 布局文件:

 FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("view/MainLayout.fxml"));
AnchorPane personOverview = (AnchorPane) loader.load();

当我在 Eclipse 中运行应用程序时一切正常,但是当我使用 Ant 部署应用程序时,出现错误:

enter image description here

构建项目后,我的文件夹结构是:

enter image description here

与部署的 jar 中的内容相同

我知道 jar 内和 eclipse 中解析路径可能存在差异,但我无法解决此问题。我使用 Java 1.8 和 JavaFX 8。

更新1根据错误,我认为它使用绝对 URL 解析路径(以 file:/C:/... 开头),但应该在 jar 文件内使用相对 url。为什么它使用绝对路径是主要问题。

更新2

有趣的是,根布局使用相同的代码成功加载..

FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();

但是内部布局初始化失败。

堆栈跟踪:

java -jar MeterChecker.jar
javafx.fxml.LoadException: unknown path

at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at com.wolf6969.checker.MainApp.showMain(Unknown Source)
at com.wolf6969.checker.MainApp.start(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/138227185.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/584634336.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/94326726.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/501263526.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/96639997.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
... 18 more
Caused by: java.lang.NullPointerException
at com.wolf6969.checker.service.SmsService.getCommandKeys(Unknown Source)
at com.wolf6969.checker.view.MainController.setupControls(Unknown Source)
at com.wolf6969.checker.view.MainController.initialize(Unknown Source)
... 27 more
Exception in Application stop method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application stop method
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$48/752848266.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at com.wolf6969.checker.MainApp.stop(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$154(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$147/1418913435.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/584634336.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/94326726.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/501263526.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/96639997.run(Unknown Source)
... 1 more
Exception running application com.wolf6969.checker.MainApp

最佳答案

我正在使用以下代码

 FXMLLoader loader=new FXMLLoader(Main.class.getResource("views/MainFrame.fxml"));
AnchorPane rootpane=(AnchorPane) loader.load();

它正在正常工作

编辑:查看您的跟踪后,我认为问题出在您的 Controller 中,请尝试看看这个 link

Caused by: java.lang.NullPointerException
at com.wolf6969.checker.service.SmsService.getCommandKeys(Unknown Source)
at com.wolf6969.checker.view.MainController.setupControls(Unknown Source)
at com.wolf6969.checker.view.MainController.initialize(Unknown Source)

关于java - 在 Eclipse 外部运行 jar 时未加载 FXML 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26329342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com