开发者

ClassLoader.getSystemResourceAsStream(className) returning null when trying to load class file resource

开发者 https://www.devze.com 2023-04-12 11:05 出处:网络
Class clazz = ...; InputStream is开发者_如何学运维 = ClassLoader.getSystemResourceAsStream(clazz.getName().replace(\'.\', \'/\') + \".class\");
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?


The Class has apparently been loaded by a different ClassLoader than the ones you're trying to find it with. Try this instead:

InputStream is = clazz.getClassLoader().getResourceAsStream(
    clazz.getName().replace('.', '/') + ".class");

Short of a flaw in the JVM, I don't think that can possibly return null.


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 ?

Class clazz = Dummy.class;
InputStream is = ClassLoader.getSystemResourceAsStream(clazz.getName().replace('.', '/') + ".class");

I mean to say that do you define clazz as ClassName.class ? If not then try doing this and then see.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号