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

4
我将使用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文件:

file:/C:/Users/Jesper/Documents/NetBeansProjects/3semester/trunk/dist/GlobalCarTrading.jar!/GUI/MainTabPane.fxml

但是该文件不在dist文件夹中,而是位于src文件夹中。如何解决这个问题?

fxml和class文件的位置: 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

1
你能在你的项目中添加类和FXML文件的位置吗? - ItachiUchiha
你不希望它查找src文件夹:因为在应用程序运行时,不能保证src文件夹甚至存在。 - James_D
1
@James_D 实际上很可能不会。 - Tim B
你能发一下.fxml文件吗?另外,你使用的是哪种项目类型?JavaFX有一个特定的项目类型可与fxml一起使用(Category= JavaFX,Type= JavaFX FXML 应用程序),值得一试。 - WillShackleford
2个回答

0

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


为什么?有任何异常吗?您可以通过添加.getContent()来获取内容,这将为您提供一个BufferedInputStream。读取字符并将它们解析为字符串。 - marcbrouwer
我得到了这些:InvocationTargetException,RuntimeException,NullPointerException。 - Jesper
它可能可以很好地读取.fxml文件,但.fxml文件本身有问题。打开.fxml文件并尝试Source-> Make Controller。 - WillShackleford
试试这个: BufferedInputStream input = (BufferedInputStream) getClass().getResource("../foo/file.ext").getContent(); String content = ""; while (input.available() > 0) { content += (char) input.read(); } - marcbrouwer
@ Jesper,也许是这样的:Parent root = FXMLLoader.load(getClass().getResource("MainTabPane.fxml").getContent()); - marcbrouwer

0

它并不是在dist文件夹中寻找文件,而是在dist文件夹中的Jar资源中寻找。Jar文件实际上是zip文件,其中可以包含文件和目录。这是寻找正确位置,因为当您分发应用程序时,用户将需要访问.fxml文件而没有源代码。源目录中的文件在构建Jar文件时存储在其中。您可以通过将MainTabPane.fxml放置在与此类相同的目录(似乎是src / GUI目录)中,使此代码正常工作。另一个选项是使用:

Parent root = FXMLLoader.load(ClassLoader.getSystemClassLoader().getSystemResource("MainTabPane.fxml"));

这也将从dist文件夹中的jar文件获取MainTabPane.fxml,但它将从jar文件中顶层目录获取它,该目录存储来自src目录而非GUI子目录内的文件。


你的意思是我应该把MainTabPane.fxml文件放在与.jar文件相同的文件夹中吗?.jar文件的路径是C:\ Users \ Jesper \ Documents \ NetBeansProjects \ 3semester \ trunk \ dist \ GlobalCarTrading.jar。 - Jesper
不,我的意思是将它放在与调用getClass()的.java文件相同的目录中。当构建jar文件时,它将从此目录复制到相应的子目录中。 - WillShackleford
你能解压缩这个jar文件并发布一个文件列表吗?同时请列出src目录中的文件列表,注意显示文件夹结构。 - WillShackleford
有可能你只需要重新构建以使事情同步。 - WillShackleford
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Jesper
META-INF目录只包含有关JAR文件的一些额外信息,这些信息可能与此问题无关。也许存在其他问题。您能否发布异常的完整文本? - WillShackleford

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接