如何查找类加载器以何种顺序加载了哪些

如何查找类加载器以何种顺序加载了哪些

本文介绍了如何查找类加载器以何种顺序加载了哪些 jar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在其他地方找不到这个问题的明确答案,所以我会在这里试试:

I could not find a clear answer to this question elsewhere, so I'll try here:

是否有某种方式(程序化的或其他的)可以获取应用程序类加载器按照加载顺序加载的 JAR/类的列表?应用程序类加载器是指在应用程序服务器(WLS、WAS、JBoss...)中加载 EAR 应用程序的类加载器,但显然,它适用于任何类加载器.

Is there some way (programmatic or other) to get a list of JARs/classes loaded by an Application Classloader in the precise order they were loaded?By Application Classloader I mean the classloader that loads an EAR application in an applications server (WLS, WAS, JBoss...), but obviously, it applies to any classloader.

所以,概括地说,我想知道的是指定类加载器加载的 JAR 的列表和顺序.不是单个类,通过调用 classloader.getPackages() 很容易找到,而是由这个类加载器加载的 JAR 文件列表.

So, to generalize, what I would like to find out is the list and order of JARs loaded by a specified classloader. Not individual classes, that is easy enough to find out by calling the classloader.getPackages(), but a list of JAR files that were loaded by this classloader.

推荐答案

简短的回答是否定的.类加载器无需公开其搜索逻辑.

The short answer is no. Classloaders are not required to expose their search logic.

但是,如果您的类加载器实例恰好是 URLClassLoader 或子类,那么您就可以通过 getURLs() 方法访问 jars/目录列表.根据此类的文档,将按顺序搜索这些 URL.

However, if your classloader instance happens to be URLClassLoader or a subclass, then you do have access to the list of jars/directories, via the getURLs() method. Per the doc for this class, those URLs will be searched in order.

在实践中,如果您试图找出从何处加载类,Steve 的回答可能更有用.

In practice, if you're trying to find out where a class is being loaded from, Steve's answer is probably more useful.

这篇关于如何查找类加载器以何种顺序加载了哪些 jar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 07:23