问题描述
有没有办法确定在运行时从哪些 JAR 加载哪些类?
Is there a way to determine which classes are loaded from which JARs at runtime?
我敢肯定我们以前都经历过 JAR 地狱.我在解决项目中的 ClassNotFoundException
s 和 NoClassDefFoundError
s 问题时遇到了很多问题.我想避免在 JAR 中查找类的所有实例,并避免对代码使用消除过程,从而导致 CNFE 找到罪魁祸首.
I'm sure we've all been in JAR hell before. I've run across this problem a lot troubleshooting ClassNotFoundException
s and NoClassDefFoundError
s on projects. I'd like to avoid finding all instances of a class in JARs and using process of elimination on the code causing a CNFE to find the culprit.
任何分析或管理工具都会为您提供此类信息吗?
Will any profiling or management tools give you this kind of information?
这个问题非常烦人,纯粹是因为我们应该在类加载时获得这些信息.必须有办法找到它,或者记录它并找到它,但我不知道有什么方法可以做到这一点,是吗?
This problem is super annoying purely because we should have this information at the time the class gets loaded. There has to be a way to get to it, or record it and find it, yet I know of nothing that will do this, do you?
我知道 OSGi 和版本化的包/模块旨在使这成为一个非问题......但它似乎不会很快消失.
I know OSGi and versioned bundles/modules aim to make this a non issue... but it doesn't seem to be going away any time soon.
注意:我发现了这个问题 是我的问题的一个子集,与从版本化 jar 加载的类有关.
Note: I found this question is a subset of my question related to classes loaded from versioned jars.
有点相关,这篇文章解释了在 JAR 中(在当前目录下)或 M2_REPO 中搜索类的策略:JarScan,扫描特定类的所有子文件夹中的所有JAR文件
Somewhat related, this post explains a strategy to search for a class within JARs (either under the current directory) or in your M2_REPO: JarScan, scan all JAR files in all subfolders for specific class
也有些相关,JBoss Tattletale
推荐答案
将 -verbose:class
开关传递给 java
命令将打印每个加载的类及其位置加载自.
Passing the -verbose:class
switch to the java
command will print each class loaded and where it was loaded from.
Joops 也是提前查找缺失类的好工具.
Joops is also a nice tool for finding missing classes ahead of time.
这篇关于如何探索从哪些 JAR 加载哪些类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!