简体   繁体   English

包含Jars的Jars中的类的替换系统类加载器

[英]Replacement System Classloader for Classes In Jars containing Jars

So far, the examples I have seen for custom ClassLoaders involve subclassing the URLClassLoader, and using that specific instance to load classes in resources. 到目前为止,我看到的自定义ClassLoader示例涉及子类化URLClassLoader,并使用该特定实例加载资源中的类。

I have tried in vain to look for alternative methods to replace the SystemClassLoader, so that my ClassLoader can be consulted for classes not located in the classpath. 我徒劳地试图寻找替换SystemClassLoader的替代方法,以便可以为不在类路径中的类查询我的ClassLoader。

I tried Thread.currentThread().setContextClassLoader , but it doesn't seem to work. 我试过Thread.currentThread().setContextClassLoader ,但它似乎不起作用。

Is it even possible? 它甚至可能吗?

Though this is an old question, there is indeed a way to replace the system ClassLoader. 虽然这是一个老问题,但确实有一种方法可以替换系统ClassLoader。 You might get more than you bargained for, however, with reflection. 然而,通过反思,你可能会得到比你讨价还价更多的东西。

        Field scl = ClassLoader.class.getDeclaredField("scl"); // Get system class loader
        scl.setAccessible(true); // Set accessible
        scl.set(null, new YourClassLoader()); // Update it to your class loader

This should work on the Oracle JVM. 这应该适用于Oracle JVM。

使用java.system.class.loader属性运行JVM:

java -Djava.system.class.loader=myClassLoader myApplication

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

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