6

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().

jaco0646
  • 15,303
  • 7
  • 59
  • 83
  • From my experience, the path of the resource is depended on what you give it. For example `/myfile.txt` will look for `{classpath}/myfile.txt`, but `myfile.txt` will look for `{classpath}/{class package path}/myfile.txt`, where `{class package path}` is the package path of the class used to request it, having said that, it will change depending on HOW you use `getResource`, for example, doing something like `this.getResource(...)` will work as described, but doing something like `SomeClass.class.getResource(...)` will not (if I'm correct, it will work as you've described). – MadProgrammer Aug 25 '15 at 23:28
  • `ClassLoader` resource paths are always absolute, but a leading `/` is **invalid**, not simply **redundant**. – Tavian Barnes Aug 26 '15 at 00:17
  • 1
    @MadProgrammer You're confusing `Class.getResource()` with `ClassLoader.getResource()`. The latter has no way of knowing about a package name. – user207421 Aug 26 '15 at 01:37
  • @TavianBarnes, can you point to some documentation? – jaco0646 Aug 28 '15 at 14:44
  • @jaco0646 I'm not aware of any official location that this is documented, except by painstakingly tracing through the source code. But the behaviour has been consistent forever. I've written an entire `java.nio.file.Path` abstraction over classpath resources just to make them more sane to work with. – Tavian Barnes Aug 31 '15 at 19:05

1 Answers1

1

Apparently not.

It is up to the class loader to interpret name in getResource(name).

Examining the source code of URLClassLoader, eventually it calls

new URL(baseURL, name)

It is important here whether name is absolute or relative

It is possible that, for some baseURL, absolute /foo/bar and relative foo/bar have the same effect. In particular, this is always the case for "jar file URL" in classpaths

baseURL:       jar:file:/home/duke/duke.jar!/

 foo/bar  ->   jar:file:/home/duke/duke.jar!/foo/bar 
/foo/bar  ->   jar:file:/home/duke/duke.jar!/foo/bar 
ZhongYu
  • 19,446
  • 5
  • 33
  • 61