繁体   English   中英

如何让NetBeans在src文件夹而不是dist文件夹中搜索我的文件?

[英]How do I make NetBeans search for my files in the src folder instead of the dist folder?

我正在使用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:\\ Users \\ Jesper \\ Documents \\ NetBeansProjects \\ 3semester \\ trunk \\ 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

它不是在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子目录。

getClass()获取调用它的类。 如果资源在同一文件夹/包中,您可以使用getResource(“file”)直接获取它。 如果文件位于父文件夹/包或不同的文件夹/包中,则可以调用getClass()。getResource(“../ foo / file”)。 这可能不是最佳解决方案,但可行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM