简体   繁体   English

加载实现另一个类的类-Java

[英]Load a class which implements another one - Java

I'm trying to load some classes dynamically by getting their bytes and using reflection to use the defineClass method. 我试图通过获取它们的字节并使用反射来使用defineClass方法来动态加载某些类。 Here is my code (this is a loop) 这是我的代码(这是一个循环)

Method m = java.lang.ClassLoader.class.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class);
            m.setAccessible(true);
            m.invoke(Main.class.getClassLoader(), className, classBytes, 0, classBytes.length);

But when a class implements another one, it throws a NoClassDefFoundError (the implemented class isn't load yet). 但是,当一个类实现另一个类时,它将引发NoClassDefFoundError(已实现的类尚未加载)。 I don't know how to do, I tried to load few times to resolve this but it doesn't work. 我不知道该怎么办,我尝试加载几次以解决此问题,但它不起作用。 I also searched to add all classes to the classpath before loading them but I don't know how to do this with an array of byte. 在加载它们之前,我还搜索了将所有类添加到类路径中,但是我不知道如何使用字节数组来执行此操作。

Dynamic class loader is what Java does. 动态类加载器是Java所做的。 There shouldn't be a need to bypass language access restrictions. 不需要绕过语言访问限制。 Subclass SecureClassLoader in the usual way. 以通常的方式将SecureClassLoader子类化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM