问题描述
我有一个很大的问题,做一些非常愚蠢的事情。也就是说打开我的META-INF文件夹中的资源文件的流。我正在研究一个jar工具,并且这样做:
I have a big issue doing something really stupid. That is to say open a stream to a resource file in my META-INF folder. I am working on a jar tool and on doing this:
InputStream schemaIS = this.getClass().getClassLoader().getResourceAsStream("/META-INF/schema.xsd");
我只是得到一个空!该项目使用maven构建,xsd文件最终在META-INF文件夹中,但它仍然无法正常工作。
I simply get a null! The project is built using maven and the xsd file ends up in the META-INF folder, but it still won't work.
我不明白的是它背后的理论? ClassLoader如何在文件系统中执行查找?如何获取文件?
What I don't understand is the theory behind it? How do the ClassLoader execute the lookup in the file system? How do I get my hands on the file?
推荐答案
尝试删除第一个斜杠:
InputStream schemaIS = this.getClass().getClassLoader().getResourceAsStream("META-INF/schema.xsd");
如果要调用 getResourceAsStream 类
对象上的$ c>方法。在 ClassLoader
对象上,路径始终是绝对路径,并且不需要前导斜杠。
You need a leading slash if you are calling the getResourceAsStream
method on a Class
object. On a ClassLoader
object, the path is always absolute, and the leading slash is not necessary.
这篇关于无法从META-INF读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!