简体   繁体   English

使用ClassLoader.defineClassCond从通过ASM创建的类的字节中获取Class对象,NoClassDefFoundException?

[英]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.

相关问题 如何从使用类加载器创建的类中调用方法 - How to call a method from class created using classloader 如何声明由类加载器加载的 class 创建的 object - How to declare an object created by a class loaded by classloader 将 java 对象/类从一个类加载器复制到另一个类加载器 - Copy java object/class from one classloader to another classloader 如何使用ASM重命名类并保留在同一类加载器中? - How to rename a class with ASM and stay in the same classloader? 如何从运行时使用ASM动态创建的Java类中获取和使用类类型? - How do I get and use a class type from a Java class that I've dynamically created at runtime using ASM? 如何从类型变量获取ClassLoader(使用泛型对类进行分区) - How to get ClassLoader from type variable (for parceling a class using generics) 重写类加载器以获取每个加载的类的字节和名称 - Overriding the classloader to get every loaded class bytes and name 如何在java中使用类加载器获取数组类? - how to get a array class using classloader in java? 从java中的ClassLoader获取.class文件 - get the .class file from ClassLoader in java 将类文件(字节)解包到 ASM ClassNode - Unpacking class file (bytes) into ASM ClassNode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM