I'm trying to utilize a resource
directory in a maven project on Netbeans 11. I can't properly access the files in the resources
directory when I build my code using Netbeans—however, it works fine when I build and run the code using mvn
via the command prompt.
More specifically, I'm trying to load an image, bern.png
, that I have stored in a directory located at /src/main/resources
. When I use the following code to try and access those resources, however, I run into a NullPointerException when toString()
is called on the result of getResource()
:
String path = this.getClass().getClassLoader().getResource("bern.png").toString();
Here is the NullPointerException stacktrace: https://pastebin.com/KqJVxWEL
However, when I build and run the project using mvn
via the command line, the project builds without issue.
Thus, the problem must lie with the way that Netbeans is viewing the resources
folder that I've created.
Here is an image of my project file tree:
And here is an image of my target build file tree:
I created the resources
directory by creating a New > Folder
in main
. As you can see, bern.png
in the resources
directory does not populate in the target build.
After building and running the project using mvn
via the command line, however, bern.png
does populate:
I've found a few articles about this, but none of them have helped. This article from javaquery refers to Netbeans 8.0, and its instructions are no longer usable as written in Netbeans 11. This StackOverflow answer refers to Netbeans 7.2, and simply creating the directory as they suggested did not function for me in Netbeans 11. This StackOverflow answer was written for Netbeans 8.0 and also did not work.
With all of that said, does anyone know how to properly create a resources
directory using NetBeans 11?