开发者

Using ClassLoader.defineClassCond to get a Class object from bytes of a class created through ASM, NoClassDefFoundException?

I've generated a class by taking the bytes of a base class and accepting them through a custom class adapter. When I call define class on the bytes produced as a product of the base class I get this error:

Caused by: java.lang.NoClassDefFoundError: com/example/MyClassBase
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    ... 24 more
Caused by: java.lang.ClassNotFoundException: com.example.MyClassBase
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 26 more

The base class has been loaded by a URLClassLoader from a jar. Is there some underlying method call in defineClass that is throwing the exception because it cannot locate the loaded class? How can I fix this?

Keep in min开发者_JAVA技巧d that it won't be practical to load the base class a different way. As a parameter to the problem I would say assume that it must be loaded through a URLClassLoader from another jar.


I think you're trying to load modified bytecode that relies on com.example.MyClassBase via a ClassLoader that can't see that class. When you create your own ClassLoader instance to load your modified class, you should be chaining it to an existing ClassLoader using its constructor to establish a parent/child hierarchy of loaders, which helps avoid this exact problem.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜