简体   繁体   English

为什么ClassLoader.getResource(“/ pkg / readme.txt”)总是返回null,资源名称带有前导斜杠?

[英]why ClassLoader.getResource(“/pkg/readme.txt”) always return null with resource name with a leading slash?

I googled a lot but still don't understand Why ClassLoader.getResource("/pkg/readme.txt") always return null? 我google了很多但仍然不明白为什么ClassLoader.getResource(“/ pkg / readme.txt”)总是返回null? otherwise without the leading slash, ClassLoader.getResource("pkg/readme.txt") can return a correct URL. 如果没有前导斜杠,ClassLoader.getResource(“pkg / readme.txt”)可以返回正确的URL。

I do understand the diff between those methods in Class and ClassLoader. 我理解Class和ClassLoader中这些方法之间的差异。

NOTE: I'm not asking the diff between Class.getResource() and ClassLoader.getResource() because I get it. 注意:我不是在问Class.getResource()和ClassLoader.getResource()之间的差异,因为我得到它。 My concern is on ClassLoader.getResource() does not work for a resource name which starts with a leading slash 我关注的是ClassLoader.getResource()不适用于以前导斜杠开头的资源名称

My concern is on ClassLoader.getResource() does not work for a resource name which starts with a leading slash 我关注的是ClassLoader.getResource()不适用于以前导斜杠开头的资源名称

That's because there are no resource names that start with a leading slash. 那是因为没有以前导斜杠开头的资源名称。 By definition a resource name is "a '/'-separated path name that identifies the resource" - the components are separated by slashes, not preceded by them. 根据定义,资源名称是“a'/' - 标识资源的分隔路径名” - 组件斜杠分隔 ,而不是以它们开头。

someClass.getResource("/pkg/readme.txt") is equivalent to someClass.getClassLoader().getResource("pkg/readme.txt") , the leading slash is not part of the resource name but rather an indication to Class.getResource that it should not prepend the class's package to the path it passes to the classloader. someClass.getResource("/pkg/readme.txt")等同于someClass.getClassLoader().getResource("pkg/readme.txt") ,前导斜杠不是资源名称的一部分,而是Class.getResource的指示Class.getResource ,它应该在类的包预先考虑到它传递给类加载器的路径。 Without the leading slash: 没有领先的斜线:

someClass.getResource("pkg/readme.txt")

would be equivalent to 相当于

someClass.getClassLoader().getResource("com/example/pkg/readme.txt")

(assuming someClass is in the package com.example ) (假设someClasscom.example包中)

Class's getResource() - documentation states the difference: Class的getResource() - 文档说明了区别:

This method delegates the call to its class loader, after making these changes to the resource name: if the resource name starts with "/", it is unchanged; 在对资源名称进行这些更改后,此方法将调用委托给其类加载器:如果资源名称以“/”开头,则它保持不变; otherwise, the package name is prepended to the resource name after converting "." 否则,在转换“。”之后,包名称将被添加到资源名称之前。 to "/". 至 ”/”。 If this object was loaded by the bootstrap loader, the call is delegated to ClassLoader.getSystemResource. 如果此对象由引导加载程序加载,则该调用将委派给ClassLoader.getSystemResource。

What is the difference between Class.getResource() and ClassLoader.getResource()? Class.getResource()和ClassLoader.getResource()有什么区别?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM