Classloader resource paths are always absolute?

前端 未结 1 1095
隐瞒了意图╮
隐瞒了意图╮ 2021-01-19 01:20

In a popular answer regarding the difference between class loading methods, Jon Skeet has stated,

Classloader resource paths are always deemed to be a

相关标签:
1条回答
  • 2021-01-19 01:50

    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 
    
    0 讨论(0)
提交回复
热议问题