ClassLoader.getSystemResourceAsStream(className) 尝试加载类文件资源时返回 null
Class clazz = ...;
InputStream is = ClassLoader.getSystemResourceAsStream(clazz.getName().replace('.', '/') + ".class");
输入流返回 null。我使用了一个简单的 java 检测代理来记录加载的类,并且该类 (clazz) 肯定是由 ClassLoader 加载的。我也尝试过
... Thread.currentThread().getContextClassLoader().getResourceAsStream(...));
,它也返回 null。类加载器无法找到资源的可能原因有哪些?
Class clazz = ...;
InputStream is = ClassLoader.getSystemResourceAsStream(clazz.getName().replace('.', '/') + ".class");
The input stream is returning null. I have used a simple java instrumentation agent to log classes as they are loaded and the class (clazz) is definitely being loaded by the ClassLoader. I've also tried
... Thread.currentThread().getContextClassLoader().getResourceAsStream(...));
and it returns null as well. What would be some possible causes for the resource not being able to be found by the class loader?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该类显然是由与您尝试查找它的类加载器不同的类加载器加载的。试试这个:
如果 JVM 没有缺陷,我认为不可能返回 null。
The Class has apparently been loaded by a different ClassLoader than the ones you're trying to find it with. Try this instead:
Short of a flaw in the JVM, I don't think that can possibly return null.
您是否尝试过 getClass().getClassLoader().getResourceAsStream()
请确保您真正要加载的类文件位于代码的类路径中。
您还可以分享 clazz.getName() 的值吗?
编辑:
你在做类似以下的事情吗?
我的意思是说您将 clazz 定义为 ClassName.class 吗?如果没有,请尝试这样做,然后看看。
Did you try getClass().getClassLoader().getResourceAsStream()
Please make sure that the class file you tru to load in in classpath of your code.
Also couild you please share the value of clazz.getName() ?
EDIT:
Are you doing something like following ?
I mean to say that do you define clazz as ClassName.class ? If not then try doing this and then see.