包含Jars的Jars中的类的替换系统类加载器 [英] Replacement System Classloader for Classes In Jars containing Jars

查看:132
本文介绍了包含Jars的Jars中的类的替换系统类加载器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我看到的自定义ClassLoader的示例涉及子类化URLClassLoader,并使用该特定实例加载资源中的类。

So far, the examples I have seen for custom ClassLoaders involve subclassing the URLClassLoader, and using that specific instance to load classes in resources.

我徒劳无功寻找替换SystemClassLoader的替代方法,以便可以为不在类路径中的类查询我的ClassLoader。

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.

我试过 Thread。 currentThread()。setContextClassLoader ,但它似乎不起作用。

I tried Thread.currentThread().setContextClassLoader, but it doesn't seem to work.

是否可能?

推荐答案

虽然这是一个老问题,但确实有一种方法可以替换系统ClassLoader。
然而,你可能会得到比你讨价还价更多的东西。反射。

Though this is an old question, there is indeed a way to replace the system 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

这应该适用于Oracle JVM。

This should work on the Oracle JVM.

这篇关于包含Jars的Jars中的类的替换系统类加载器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆