Monday, September 5, 2011

From which Jar is my class loaded?

Ever wondered from which Jar is a class loaded at runtime?
I am talking about class loading here. That is, if there is a class (say, com.test.Class1)  which is in MyJar1.jar and MyJar2.jar. Each of these Jars are different classpath levels.

Now in your class, execute the below code and you can see the complete path to the Jar file from which the Class is loaded:


CodeSource resource = WSDLFactory.class.getProtectionDomain().getCodeSource(); if (resource != null)      System.out.println("The URL --> " + resource.getLocation());


I used above code to know from which wsdl4j.jar did the WSDLFactory class got loaded. I had wsdl4j.jar in 3 places, each was of different version. I had to identify the jar which was used at runtime. Thanks to my lead to teach me this :)