How to access a resource from a java library packed into its own .jar

Viewed 8483

I have a library project with some resources that I would like to load and use. I would be using this library in another project so I have to pack the resources into the .jar. This is how that's achieved:

<resources>
    <resource>
        <directory>src/main/resources</directory>
    </resource>
</resources>

The problem comes when trying to retrieve the file, I do:

String resourceName = "myTemplateResource.json";
URL url = MyClass.class.getClassLoader().getResource(resourceName);

url is assigned a null value. It happens in the library's tests and in the dependant project. Any hint of what is missing?

Also tested using

MyClass.class.getResource("/myTemplateResourceName.json");

and

MyClass.class.getClassLoader().getResource("src/main/resources/myTemplateResource.json");

with the same result.

2 Answers
Related