Find The Jar!
Date: Jul 31, 2010 @ 20:23
One of my friend (Ankit) called me up yesterday and asked if I could tell that from which location(or jar) a specific class is loaded. His problem was that there were two versions of a jar file was present in classpath and he wanted to know how to identify or pin point the exact that that is being loaded so that he can check the version of the jar.
Later that day I got solution from the same friend(with the help of Gagandeep). Here is the simple program to check.
import java.security.*;
import java.net.*;
import com.sun.crypto.provider.AESCipher;
class Test{
public static void main(String... args){
//Loaded from bootstrap class loader(say rt.jar) and extension class loader(jre/lib/ext).
URL location = String.class.getResource('/'+String.class.getName().replace('.',
'/')+".class");
System.out.println(location);
URL location1 = AESCipher.class.getResource('/'+AESCipher.class.getName().replace('.',
'/')+".class");
System.out.println("AESCipher:"+location1);
//System class loader from classpath location.
CodeSource cs =
org.apache.commons.collections.CollectionUtils.class.getProtectionDomain().getCodeSource();
URL jar = cs.getLocation();
System.out.println("Location of class is:"+ jar);
}
}
Output is:-
jar:file:/C:/Program%20Files/Java/jre6/lib/rt.jar!/java/lang/String.class
AESCipher:jar:file:/C:/Program%20Files/Java/jre6/lib/ext/sunjce_provider.jar!/com/sun/crypto/provider/AESCipher.class
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections/CollectionUtils
at Test.main(Test.java:16)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.CollectionUtils
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
Now add Apache common collection jar in the classpath.
set classpath=./commons-collections-2.1.1.jar;
then run the program:
Output is:-
jar:file:/C:/Program%20Files/Java/jre6/lib/rt.jar!/java/lang/String.class
AESCipher:jar:file:/C:/Program%20Files/Java/jre6/lib/ext/sunjce_provider.jar!/com/sun/crypto/provider/AESCipher.class
Location of class is:file:/D:/Blogs/find_the_jar/commons-collections-2.1.1.jar
import java.security.*;
import java.net.*;
import com.sun.crypto.provider.AESCipher;
class Test{
public static void main(String... args){
//Loaded from bootstrap class loader(say rt.jar) and extension class loader(jre/lib/ext).
URL location = String.class.getResource('/'+String.class.getName().replace('.',
'/')+".class");
System.out.println(location);
URL location1 = AESCipher.class.getResource('/'+AESCipher.class.getName().replace('.',
'/')+".class");
System.out.println("AESCipher:"+location1);
//System class loader from classpath location.
CodeSource cs =
org.apache.commons.collections.CollectionUtils.class.getProtectionDomain().getCodeSource();
URL jar = cs.getLocation();
System.out.println("Location of class is:"+ jar);
}
}
Output is:-
jar:file:/C:/Program%20Files/Java/jre6/lib/rt.jar!/java/lang/String.class
AESCipher:jar:file:/C:/Program%20Files/Java/jre6/lib/ext/sunjce_provider.jar!/com/sun/crypto/provider/AESCipher.class
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections/CollectionUtils
at Test.main(Test.java:16)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.CollectionUtils
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
Now add Apache common collection jar in the classpath.
set classpath=./commons-collections-2.1.1.jar;
then run the program:
Output is:-
jar:file:/C:/Program%20Files/Java/jre6/lib/rt.jar!/java/lang/String.class AESCipher:jar:file:/C:/Program%20Files/Java/jre6/lib/ext/sunjce_provider.jar!/com/sun/crypto/provider/AESCipher.class Location of class is:file:/D:/Blogs/find_the_jar/commons-collections-2.1.1.jar
