我使用JavaFX
创建了一个应用程序。每当我尝试运行它时,我都会得到一个LoadException
。问题是,当我尝试运行此代码时,例如:
public void start(Stage stage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("MainTabPane.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
stage.setTitle("Global Car Trading");
}
它使用以下路径搜索MainTabPane.fxml:
文件:/ C:!/Users/Jesper/Documents/NetBeansProjects/3semester/trunk/dist/GlobalCarTrading.jar /GUI/MainTabPane.fxml
但该文件不在dist文件夹中,它位于src
文件夹中。我该如何解决这个问题?
fxml和类文件的位置: C:\用户\的Jesper \文件\的NetBeansProjects \ 3semester \树干\ SRC \ GUI
当代码
时Parent root = FXMLLoader.load(getClass().getResource("GUI/MainTabPane.fxml"));:
我得到以下例外情况:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:363)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
at com.sun.javafx.application.LauncherImpl$$Lambda$48/1268447657.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3201)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3169)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3142)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3118)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3098)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3091)
at GUI.GlobalCarTrading.start(GlobalCarTrading.java:23)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/1715882599.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/128893786.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1127003017.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/1108411398.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/1147985808.run(Unknown Source)
... 1 more
Exception running application GUI.GlobalCarTrading
当我删除" GUI /"部分,像这样:
Parent root = FXMLLoader.load(getClass().getResource("MainTabPane.fxml"));:
我也有这个例外:
Caused by: javafx.fxml.LoadException: file:/C:/Users/Jesper/Documents/NetBeansProjects/3semester/trunk/dist/GlobalCarTrading.jar!/GUI/MainTabPane.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2595)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2435)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3208)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3169)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3142)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3118)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3098)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3091)
at GUI.GlobalCarTrading.start(GlobalCarTrading.java:23)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/281867444.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/128893786.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/170597113.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/1108411398.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/1147985808.run(Unknown Source)
... 1 more
答案 0 :(得分:0)
它不会在dist文件夹中查找文件,它正在dist文件夹中的Jar中查找资源。 Jar文件实际上是zip文件,可以包含文件和目录。这是它看起来正确的地方,因为当您分发应用程序时,用户将需要访问.fxml文件而不是您的源。构建jar时,源目录中的文件存储在jar文件中。您可以通过将MainTabPane.fxml放在与此类(显示为src / GUI目录)相同的目录中来使此代码生效。另一种选择是使用:
Parent root = FXMLLoader.load(ClassLoader.getSystemClassLoader().getSystemResource("MainTabPane.fxml"));
这也将从dist文件夹中的jar文件中获取MainTabPane.fxml,但它将从jar文件中的顶级目录获取它,其中存储来自src目录的文件而不是jar文件中的GUI子目录
答案 1 :(得分:0)
getClass()获取调用它的类。如果资源位于同一个文件夹/包中,您可以使用getResource(" file")直接获取它。如果文件位于父文件夹/包或不同的文件夹/包中,则可以调用getClass()。getResource(" ../ foo / file")。这可能不是最佳解决方案,但可行。