How To Check Where The Class Loaded From

This example shows to check the path of the class loaded from. This utility would be helpful in certain cases if you want to check the class loading path when there is confusion over multiple libraries exist in the different path. To do that, the below example use the following steps:

  • Get the Class for the actual class to be checked for the path
  • Invoke the getProtectionDomain() nethod of the Class instance which returns the ProtectionDomain object which is originally hold the characteristics of the domian.
  • Then call the getCodeSource() method which returns the CodeSource object
  • Then call the getLocation() method of the CodeSource instance which returns the path of the class loaded.

Lets look at the example:



public class JavaCoreExample {
	public static void main(String[] args) {
		// get the runtime class of this Object
		Class<?> cls = new JavaCoreExample().getClass();
		ProtectionDomain pDomain = cls.getProtectionDomain();
		CodeSource cSource = pDomain.getCodeSource();
		// get the location associated with this CodeSource
		URL location = cSource.getLocation();
		System.out.println("Location: " + location);


Location: file:/home/krishna/workspace/Java%20Project/bin/



About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.