2

Simple question, how to include and load embedded resources from a Maven based project when using Netbeans 12x

In a traditional/Ant based project, it's possible to simply place embedded resources within the Source Package and load it using Class#getResource, but for some reason, a Maven based project does not work the same way.

The following code works when using Ant, but fails when using Maven:

import java.io.IOException;
import javax.imageio.ImageIO;

public class Main {

    public static void main(String[] args) throws IOException {
        new Main();
    }

    public Main() throws IOException {
        ImageIO.read(getClass().getResource("/images/Background.jpg"));
    }

}

So, the, simple, question is, how to include embedded resources when using Maven with Netbeans 12x

Harald K
  • 26,314
  • 7
  • 65
  • 111
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • [1] Is this issue specific to NetBeans 12.x, or does it also exist in any earlier NetBeans releases? I'm asking because one of the changes in NetBeans 12 was to implement [NETBEANS-4666: Upgrade exec-maven-plugin from 1.5.0 to 3.0.0](https://issues.apache.org/jira/browse/NETBEANS-4666), and I've noticed (but not yet researched) a couple of other issues with Maven projects in NB 12.x that don't exist in NetBeans 11.x. [2] It might be worth detailing how the code _"fails when using Maven"_ in the question. – skomisa Sep 24 '21 at 09:41
  • I'm not sure, I jumped 3 or versions and we're had so many questions on the subject recently, I figured I do a post on how I "solved" the issue ;) – MadProgrammer Sep 24 '21 at 09:52
  • I think you should remove the reference to NetBeans as it's a maven issue not a NetBeans issue. – asbachb Sep 25 '21 at 04:19
  • Not sure, as the IDE right now is the key component to both question and answer – MadProgrammer Sep 25 '21 at 05:18
  • @asbachb See my earlier comment. I disagree that it is a Maven issue rather than a NetBeans issue. Problems only arise (for me) with NetBeans 12.x, when the Maven plugin was changed. Without knowing the root cause of the problem it isn't possible or helpful to target NetBeans, or Maven, or the Maven plugin as the the guilty party. – skomisa Oct 23 '21 at 16:20

1 Answers1

3

Maven has its own requirements with regards to where sources files and "other resources" are stored. Unlike Ant, you can't simply drop "resources" into the src/main/java directory, instead, you need to add them to the src/main/resources directory.

For some reason, however, Netbeans doesn't seem to create this directory for you (at least not for the base "application" template I was using).

In this case, you need to create it yourself. You can do this manual from the command line or file manager, but Netbeans will allow you to create as well.

Start by right clicking on the root node of the project and select "New/Folder"

ProjectFolder

From the dialog, name the folder resources and then tap the "Browse" button (next to "Parent Folder"

Options

Select the src/main directory

DestinationFinally

Tap finish and a new "node" should appear in the project called "Other Sources"

Finally

Now, I wish I could say it was easy to add resources to this directory, but I just end up copying and pasting files directly to the directory using a file manager.

Once you have the resource included (and they should show up under the "Other Sources" node), you can reference them from your code.

The important thing to remember though is, you don't need to include resources in the path, as the contents of the directory are copied into the resulting product, not the directory itself.

So, with a layout of:

Final layout

The following code will load the image without issues:

import java.io.IOException;
import javax.imageio.ImageIO;

public class Main {

    public static void main(String[] args) throws IOException {
        new Main();
    }

    public Main() throws IOException {
        ImageIO.read(getClass().getResource("/images/Background.png"));
    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366