In a popular answer regarding the difference between class loading methods, Jon Skeet has stated,
Classloader resource paths are always deemed to be absolute.
An even more popular answer affirms this statement with an example.
ClassLoader.getResourceAsStream(path)
will consider all paths to be
absolute paths. So calling
String.getClassLoader().getResourceAsString("myfile.txt")
and
String.getClassLoader().getResourceAsString("/myfile.txt")
will both
look for a file in your classpath at the following location:
./myfile.txt
.
Disregarding the fact that the example won't compile, consensus indicates a leading forward slash is irrelevant to the ClassLoader.
A simple test shows otherwise.
Foo.class.getClassLoader().getResource("test.xml") // file
Foo.class.getClassLoader().getResource("/test.xml") // null
I have simply added the path files/test.xml
to the classpath of a test project, with a class named Foo
containing a main()
method printing out the results of those two calls. What am I missing, since hundreds of people have up-voted the aforementioned answers? Are ClassLoaders absolute, or do they depend on the structure of the input name?
This Oracle blog post was helpful to my understanding.
The method which ultimately constructs a URL from the resource name is URLClassPath.JarLoader.checkResource().
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…