获取指定的类属于哪个jar包

方法一、Eclipse 工具

  Ctrl + Shift + T,即可。

 

方法二、参见 https://stackoverflow.com/questions/1983839/determine-which-jar-file-a-class-is-from

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");

As notnoop pointed out klass.getResource() 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

The getProtectionDomain().getCodeSource().getLocation() 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
posted @ 2018-10-19 11:02  flyer999  阅读(206)  评论(0编辑  收藏  举报