Java有没有办法加载一个已被System ClassLoader加载的类不同的类?
作者:互联网
(可怕的标题,我知道)
解释这个的最好方法是,我需要加载一些将在运行时从URLClassLoader加载的类,这些类引用了已经由System ClassLoader加载的类的实例,并且它们无法重新加载,因为其他一些问题.
此外,我愿意更改我的代码,以便我可以使用System ClassLoader从类路径中的jar加载类,但我无法做到这一点.
解决方法:
不是.每个类加载器都将引导类加载器作为祖先,并且尝试加载已经在类加载器中定义的类只会导致使用祖先的版本.这是为了防止重新定义java.lang.Object和其他内在函数.
The
ClassLoader
class uses a delegation model to search for classes and resources. Each instance ofClassLoader
has an associated parent class loader. When requested to find a class or resource, aClassLoader
instance will delegate the search for the class or resource to its parent class loader before attempting to find the class or resource itself. The virtual machine’s built-in class loader, called the “bootstrap class loader”, does not itself have a parent but may serve as the parent of aClassLoader
instance.
您可以定义一个自定义类加载器,它暴露defineClass而不会被findClass调用,并避免在findClass中发生的委托,但是当parent.findClass(我不会依赖于所有JVM上的预期的defineClass(className,bytes)) className)存在.
标签:java,system,classloader,urlclassloader 来源: https://codeday.me/bug/20190902/1791281.html