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

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

问题描述

到目前为止,我看到的自定义类加载器示例涉及对 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.

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

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