1

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:

An image of my project file tree

And here is an image of my target build file tree:

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:

Image of target build file tree after building with mvn

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?

Rvby1
  • 81
  • 2
  • 9
  • You have to explicitly include resources in the pom.xml file. see https://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html – WillShackleford Oct 11 '19 at 20:07

2 Answers2

0

The issue wasn't that Netbeans was viewing the directory incorrectly—the problem was that Netbeans 11's default Maven build wasn't recognizing the directory correctly. Switching Netbeans's Maven system to the most recent binary from the Maven website addressed the issue.

Rvby1
  • 81
  • 2
  • 9
  • I find your answer surprising. What exactly was _"Netbeans 11's default Maven"_, and what do you mean by _"default Maven build wasn't recognizing the directory **correctly**"_? You seem to be suggesting that you were initially using a "bad" version of Maven, which seems unlikely for this particular issue. Is that the case, and are you certain that all you did was change the version of Maven used by NetBeans? Showing your `pom.xml` would also be helpful if any else experiences your problem. – skomisa Oct 12 '19 at 02:55
0

I had the same problem using a Maven project under Netbeans 11.3 for the Netty client/server framework example http2/tiles. Because using the source files from netty-all-4.1.50.Final-sources.jar, the jpg resource files were located inside the directory io/netty/example/http2/tiles together with the java files. Copying this directory to the Netbeans Maven project's subdirectory src/main/java will result in the problem, that these resource files are not included in the target jar file.

The solution is to locate the resource files in a subdirectory main/resources as it is done in the Netty Github repository https://github.com/netty/netty/tree/4.1/example/src/main. Resource files in this location are automatically added to the target jat file when the project is build.

Manfred Steiner
  • 1,215
  • 2
  • 13
  • 27