简体   繁体   English

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

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

I'm using JavaFX to create an application. 我正在使用JavaFX来创建一个应用程序。 Every time time I try to run it, I get a LoadException . 每次我尝试运行它,我都会得到一个LoadException The problem is that when I try to run this code, for example: 问题是,当我尝试运行此代码时,例如:

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");
}

It searches for the MainTabPane.fxml using this path: 它使用以下路径搜索MainTabPane.fxml:

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

But the file is not located in the dist folder, it's located in the src folder. 但该文件不在dist文件夹中,它位于src文件夹中。 How do I fix this? 我该如何解决?

Location of fxml and class files: C:\\Users\\Jesper\\Documents\\NetBeansProjects\\3semester\\trunk\\src\\GUI fxml和类文件的位置:C:\\ Users \\ Jesper \\ Documents \\ NetBeansProjects \\ 3semester \\ trunk \\ src \\ GUI

When the code is 当代码是

Parent root = FXMLLoader.load(getClass().getResource("GUI/MainTabPane.fxml"));:

I get the following exceptions: 我得到以下例外情况:

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

When I remove the "GUI/" part, like this: 当我删除“GUI /”部分时,如下所示:

Parent root = FXMLLoader.load(getClass().getResource("MainTabPane.fxml"));:

I also get this exception: 我也得到这个例外:

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

It is not looking for the file in the dist folder it is looking for the resource in the Jar in the dist folder. 它不是在dist文件夹中查找文件,它正在寻找dist文件夹中Jar中的资源。 Jar files are actually zip files and can have files and directories within them. Jar文件实际上是zip文件,可以包含文件和目录。 This is the correct place for it to look since when you distribute the application the user will need access to the .fxml file and not have your source. 这是它看起来正确的地方,因为当您分发应用程序时,用户将需要访问.fxml文件而不是您的源。 Files in you source directory are stored in the jar file when the jar is built. 构建jar时,源目录中的文件存储在jar文件中。 You can make this code work by placing the MainTabPane.fxml in the same directory as this class which appears to be the src/GUI directory. 您可以通过将MainTabPane.fxml放在与此类(显示为src / GUI目录)相同的目录中来使此代码生效。 Another option would be to use: 另一种选择是使用:

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

This will also get MainTabPane.fxml from the jar file in the dist folder, but it will get it from the top-level directory within the jar file where files from the src directory are stored instead of the GUI subdirectory within the jar. 这也将从dist文件夹中的jar文件中获取MainTabPane.fxml,但它将从jar文件中的顶级目录获取它,其中存储来自src目录的文件而不是jar中的GUI子目录。

getClass() gets the class from which it is called. getClass()获取调用它的类。 If the resource is in the same folder/package you can directly get it with getResource("file"). 如果资源在同一文件夹/包中,您可以使用getResource(“file”)直接获取它。 If the file is in a parent folder/package or in a different folder/package you can call getClass().getResource("../foo/file"). 如果文件位于父文件夹/包或不同的文件夹/包中,则可以调用getClass()。getResource(“../ foo / file”)。 This might not be the best solution but it works. 这可能不是最佳解决方案,但可行。

暂无
暂无

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

相关问题 我应该删除我在netbeans中制作的项目的dist文件夹吗? - Should i delete dist folder of my project made in netbeans? 如何访问SRC文件夹中的文件? - How can I access files in my SRC folder? 我将前端文件放在哪个文件夹中 Spring 引导。 是 Src/main/resource 还是 Src/main - Which Folder do i put my frontend files in Spring boot. Is it Src/main/resource or Src/main Java — 如何访问(导入)与 src 文件夹位于同一目录中的文件夹中的 class 文件? - Java — how do I access (import) the class files in a folder that is in the same directory as the src folder? Netbeans没有在dist文件夹中构建lib文件夹 - Netbeans not building the lib folder in the dist folder 如何从dist文件夹Java中读取文件 - How to read files from dist folder java 如何使用Netbeans将文件夹包含到dist JAR中作为构建过程的一部分? - How can I include a folder into the dist JAR as part of the build process using Netbeans? 我如何在 Eclipse 上从 src 文件夹编译和运行简单的 java 文件? - how do i compile and run simple java files on eclipse from a src folder? 如何将文件添加到文件夹? 爪哇 - How do I add files to my folder? java 如何使用 build.xml 将 dist 文件夹中的 jar 文件和 dist 文件夹内 lib 中的 jar 文件组合成单个 jar 文件 - how to combine jar files from dist folder and jar files in lib within dist folder to make a single jar file using build.xml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM