getClass()。getResourceAsStream(" / folder")返回null [英] getClass().getResourceAsStream("/folder") return null
问题描述
我尝试使用
getClass()列出jar文件夹文件.getResourceAsStream(/ folderName)
它返回 null
但是 getClass()。getResourceAsStream(folderName / fileName )
不 null
有什么区别?
它在eclipse中有效但不在 jar
文件中
it works in eclipse but not in jar
file
推荐答案
您告诉类加载器找到/ folderName
中的文件(资源),它是根(绝对)。虽然folderName
是您班级所在的当前文件夹(相对于)。
You're telling the Class Loader to locate the file (resource) in "/folderName"
which is the root (absolute). While "folderName"
is the current folder (relative to) your class is located in.
Plus:
-
getClass()。getResourceAsStream()
正在使用System Class Loader。不是加载父类的类加载器。
getClass().getResourceAsStream()
is using the System Class Loader. Not the class loader that loaded the parent class.
使用 Example.class.getResourceAsStream()
和您应该能够访问资源。
Use Example.class.getResourceAsStream()
and you should be able to access the resources.
这篇关于getClass()。getResourceAsStream(" / folder")返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!