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

时间:2014-10-12 19:54:54

标签: java eclipse ant javafx fxml

我尝试加载我的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(以文件:/ 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

1 个答案:

答案 0 :(得分:1)

我正在使用以下代码

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

它正在按原样运作

在查看您的跟踪后,

编辑我认为问题出现在您的控制器中,请尝试查看此信息 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)