[英]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:
当我对作为基类的乘积生成的字节调用define类时,出现此错误:
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. URLClassLoader已从jar中加载了基类。 Is there some underlying method call in defineClass that is throwing the exception because it cannot locate the loaded class?
defineClass中是否存在一些引发异常的底层方法调用,因为它无法找到已加载的类? How can I fix this?
我怎样才能解决这个问题?
Keep in mind 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.
作为问题的参数,我想假设它必须通过URLClassLoader从另一个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. 我认为您正在尝试通过看不到该类的ClassLoader加载依赖com.example.MyClassBase的修改后的字节码。 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.
创建自己的ClassLoader实例以加载修改后的类时,应使用其构造函数将其链接到现有的ClassLoader,以建立加载器的父/子层次结构,这有助于避免此确切问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.