Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 Java and using reflection to get the object to tell me its own jar file.

Thanks.

like image 207
Chris Avatar asked Jan 04 '10 14:01

Chris


People also ask

How can I tell what class a JAR file is?

To find the . jar files that contain a class, you can use the FindClass.sh script.

Where is JAR file located?

A JAR file may contain a manifest file, that is located at META-INF/MANIFEST. MF . The entries in the manifest file describe how to use the JAR file. For instance, a Classpath entry can be used to specify other JAR files to load with the JAR.


2 Answers

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
like image 190
Thomas Jung Avatar answered Oct 24 '22 08:10

Thomas Jung


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.

like image 45
Carl Smotricz Avatar answered Oct 24 '22 09:10

Carl Smotricz