确定类来自哪个JAR文件 [英] Determine which JAR file a class is from
本文介绍了确定类来自哪个JAR文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我现在不在IDE的前面,只需查看API规范。
I am not in front of an IDE right now, just looking at the API specs.
CodeSource src = MyClass.class.getProtectionDomain().getCodeSource();
if (src != null) {
URL jar = src.getLocation();
}
我想确定类是从哪个JAR文件。这是做到的方法吗?
I want to determine which JAR file a class is from. Is this the way to do it?
推荐答案
是的。它适用于所有类,除了由bootstrap classloader加载的类。另一种确定方法是:
Yes. It works for all classes except classes loaded by bootstrap classloader. The other way to determine is:
Class klass = String.class;
URL location = klass.getResource('/' + klass.getName().replace('.', '/') + ".class");
$ b $ p < 方法返回类文件本身的位置。例如:
As notnoop pointed out getProtectionDomain().getCodeSource().getLocation()
method returns the location of the class file itself. For example:
jar:file:/jdk/jre/lib/rt.jar!/java/lang/String.class
file:/projects/classes/pkg/MyClass$1.class
$ c> klass.getResource()方法返回jar文件或CLASSPATH的位置
The klass.getResource()
method returns the location of the jar file or CLASSPATH
file:/Users/home/java/libs/ejb3-persistence-1.0.2.GA.jar
file:/projects/classes
这篇关于确定类来自哪个JAR文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文