如何列出特定类加载器中加载的所有类
出于调试原因和好奇心,我希望列出加载到特定类加载器的所有类。
鉴于类加载器的大多数方法都受到保护,那么完成我想要的事情的最佳方法是什么?
谢谢!
For debug reasons, and curiosity, I wish to list all classes loaded to a specific class loader.
Seeing as most methods of a class loader are protected, what is the best way to accomplish what I want?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个。这是一个黑客解决方案,但它可以。
任何类加载器中的
classes
字段(自 1.0 起在 Sun 的 impl 下)都保存对加载器定义的类的硬引用,因此它们不会被 GC 回收。您可以通过反思受益。Try this. It's a hackerish solution but it will do.
The field
classes
in any classloader (under Sun's impl since 1.0) holds hard reference to the classes defined by the loader so they won't be GC'd. You can take a benefit from via reflection.Instrumentation.getInitiatedClasses(ClassLoader)
可能会做你想做的事。根据文档:
我不确定“启动加载程序”是什么意思。如果这没有给出正确的结果,请尝试使用 getAllLoadedClasses() 方法并通过 ClassLoader 手动过滤。
如何获取
Instrumentation
的实例只有代理 JAR(与应用程序 JAR 分离)才能获取
Instrumentation
接口的实例。使其可供应用程序使用的一种简单方法是创建一个代理 JAR,其中包含一个带有premain
方法的类,该方法除了保存对Instrumentation
实例的引用之外什么也不做。系统属性。代理类示例:
清单示例:
应用程序必须引用生成的 JAR,并且在启动应用程序时在命令行(使用 -javaagent 选项)指定。它可能会在不同的 ClassLoader 中加载两次,但这不是问题,因为系统 Properties 是每个进程的单例。
示例应用程序类
Instrumentation.getInitiatedClasses(ClassLoader)
may do what you want.According to the docs:
I'm not sure what "initiating loader" means though. If that does not give the right result try using the
getAllLoadedClasses()
method and manually filtering by ClassLoader.How to get an instance of
Instrumentation
Only the agent JAR (which is separate from the application JAR) can get an instance of the
Instrumentation
interface. A simple way to make it available to the application is to create an agent JAR containing one class with apremain
method that does nothing but save a reference to theInstrumentation
instance in the system properties.Example agent class:
Example manifest:
The resulting JAR must then be referenced by the application and specified on the command line (with the
-javaagent
option) when launching the application. It might be loaded twice in differentClassLoader
s, but that is not a problem since the systemProperties
is a per-process singleton.Example application class