I have a jar (foo.jar) with a file in it's root - 1.txt.
I tried to run the following command -
MyTestClass.class.getClassLoader().getResource("/1.txt");
The jar (foo.jar) was all I had in the classpath.
I expected this to work as the "/" is supposed to lead me to the root of the classpath/jar (according to other answers I've read in stackoverflow). This returns null.
But it seems that the leading "/" isn't working as expected for me.
I figured it out and it worked without the / -
MyTestClass.class.getClassLoader().getResource("1.txt") --> jar:file:/tmp/myjar.jar!/1.txt
What is the effect/purpose of leading "/" in getResouce?
class.getResource()
(uses relative paths) andclass.getClassLoader().getResource()
(uses absolute paths)