1

I've seen some posts on using java.lang.Class.getResources() and java.lang.Class.getResourcesAsStream() on SO today. Somehow, I still have confusion.

I have a Jar file that contains this structure (resources/test.xml)

This jar file is on the classpath of my application, and when I call below piece of code, it returns null, i.e. value of mappingURL is null.

 URL mappingURL = this.getClass().getResource("/resources/test.xml");

However when I store the XML file in exploded format on the classpath i.e. by creating a directory "resources" and storing mapping.xml inside, it works.

I'm using this URL for reading the content of the "test.xml" file later.

Does that mean, getResources() is not the appropriate method for reading the files from inside a Jar? I didn't understand why mappingURL is null when file (test.xml) is present in the Jar file?

3 Answers 3

3

The getResource() method will return null if it cannot find the resource. You are prefixing the resource with a / which means that it is trying to look in the folder. You should be able to remove the leading / and achieve your intended result.

Here is the getResource() method description from the documentation:

Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.

The name of a resource is a '/'-separated path name that identifies the resource.

This method will first search the parent class loader for the resource; if the parent is null the path of the class loader built-in to the virtual machine is searched. That failing, this method will invoke findResource(String) to find the resource.

Sign up to request clarification or add additional context in comments.

2 Comments

prefixing with "/" should look for the file in classpath. I somewhere read that If I do not prefix with "/" it looks for the file inside the same folder from which class belong. So if your file that you're trying to look for is somewhere else and not in the same package hierarchy of your calling class, you shd use "/" as prefix. Is this correct?
If you do not prefix with "/" then it will look inside the package to which the class belongs. If you want to look in the /resources/xmlfiles folder you would do getResource('/resources/xmlfiles/myXmlFile.xml');
1
this.getClass().getResource("/resources/test.xml");

That should work, provided that the class file you are starting with is also in the same JAR file.

2 Comments

So, scope of getResources() is just the jar from which its called?
Not exactly. It uses the same classloader as the class, so it includes that jar, and any parent classloaders (but not completely unrelated jars).
0

Does that mean, getResources() is not the appropriate method for reading the files from inside a Jar?

This is correct. If your resource will be bundled in a jar, you always want to use getResourceAsStream().

A single line later and you can start reading the file:

BufferedReader reader = new BufferReader(InputStreamReader(getResourceAsStream("/test.xml")));

reader.readLine();
//...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.