在Eclipse外运行jar时,FXML布局不加载 [英] FXML layout not loaded when run jar outside Eclipse
问题描述
我尝试加载我的fxml布局文件如下:
I try to load my fxml layout file as follows:
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("view/MainLayout.fxml"));
AnchorPane personOverview = (AnchorPane) loader.load();
当我在Eclipse中运行应用程序时,所有的工作,但是当我使用Ant部署应用程序时,我已经一个错误:
When I run application in Eclipse all works, but when I deployed application with Ant, I've got an error:
建立项目后,我的文件夹结构是:
After building project my folder structure is:
与部署的jar相同
我知道在jar和eclipse中解析路径可能有差异,但是我无法解决这个问题。
我使用Java 1.8和JavaFX 8.
I understand that there might be a problem with difference in resolving path inside jar and in eclipse, but I can't resolve this. I use Java 1.8 and JavaFX 8.
更新1
承认错误我认为它解决路径使用绝对URL(以文件开头:/ C:/ ...),但应该在jar文件中使用相对url。为什么使用绝对路径是主要问题。
UPDATE 1 Accodring to the error I think it resolves path using absolute URL (starts with file:/C:/...), but should use relative url inside jar file. Why it use absolute path is main question.
更新2
有趣的是root布局加载成功,使用相同的代码。
Interesting that root layout loads successfully, with same code..
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
但内部布局初始化失败。
But inner layout initialization fails.
堆栈跟踪:
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();
它正在工作,因为它应该
and it is working as it should
编辑:查看你的跟踪我认为问题在你的控制器,尝试看看这个
链接
edit : after looking at your trace I think problem is in your controller , try having a look at this 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)
这篇关于在Eclipse外运行jar时,FXML布局不加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!