使用ClassLoader.defineClassCond从通过ASM创建的类的字节中获取Class对象,NoClassDefFoundException?
我通过获取基类的字节并通过自定义类适配器接受它们来生成一个类。当我对作为基类的产品生成的字节调用定义类时,我收到此错误:
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
基类已由 URLClassLoader 从 jar 加载。 DefineClass 中是否有一些底层方法调用因无法找到加载的类而引发异常?我该如何解决这个问题?
请记住,以不同的方式加载基类是不切实际的。作为问题的参数,我想说假设它必须通过 URLClassLoader 从另一个 jar 加载。
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您正在尝试通过无法看到该类的 ClassLoader 加载依赖于 com.example.MyClassBase 的修改后的字节码。当您创建自己的 ClassLoader 实例来加载修改后的类时,您应该 使用其构造函数将其链接到现有的 ClassLoader 来建立加载器的父/子层次结构,这有助于避免这个确切的问题。
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.