How to have an object/class tell me what jar file it is from
In Eclipse and a class is being loaded that should not be possible. In debug mode, I can pause it and see a call to class a.b.c BUT a.b.c class should exist anywhere since it has been renamed. My assumption is that there is an old jar file being called...but I cannot find it. So how do I find the jar file that class a.b.c is in while in debug mode in eclipse ? Either through an eclipse menu option or via Jav开发者_如何学运维a and using reflection to get the object to tell me its own jar file.
Thanks.
You can use the class loader to retrieve the URL of a resource. This works also for classes. To get the location of the java.lang.String
class:
X.class.getClassLoader().getResource("java/lang/String.class");
Output:
jar:file:/C:/Program%20Files/Java/jdk1.6.0_17/jre/lib/rt.jar!/java/lang/String.class
If you fire up your JVM using -verbose:class (you can set this as a JVM option in Eclipse) you can see exactly which classes are being loaded when, and I think from where.
There are a number of circumstances in which a compiled .class
file remains available to Eclipse (and its JVM) although you've removed or renamed the source.
To ensure this isn't fooling you, you can either run Project|Clean
in Eclipse or simply go into the Navigator view and delete your classes directory (standard is bin
), then let Eclipse re-build it.
Can you "F3" (open the class)? Even if it doesn't decompile it should show you the jar file.
If there is an old jar somewhere, you can ask the JAR Class Finder to find it by specifying the class you are looking for.
It is an independent (i.e. not involving some extra code) way to find that jar.
You can also use the "Open Type" dialog (Ctrl+Shift+T
). Type the name of the class, and if Eclipse finds it, it'll show the jar(s) it came from.
Note, however, that the classpath that Eclipse sees is not necessarily identical to the runtime classpath.
Thanks for your help, the solution from Thomas Jung was to:
X.class.getClassLoader().getResource("java/lang/String.class");
精彩评论