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/

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!