Write a Java program to print the class loader of the given class


This is a Java program that demonstrates how to get the class loader associated with a class using the getClassLoader() method.

In the main() method, the program gets the Class object associated with the ClassLoader class using the Class.forName() method. It also gets the Class object associated with the java.lang.String class and the int primitive type. The int primitive type can be accessed directly as a class using int.class.

The program then uses the getClassLoader() method of each Class object to get the class loader associated with that class. The class loader is printed to the console using the println() method

Source Code

public class ClassLoader
{
	public static void main(String[] args) throws ClassNotFoundException
	{
		Class c1 = Class.forName("ClassLoader");
		Class c2 = Class.forName("java.lang.String");
		Class c3 = int.class;
 
		System.out.println("Class loader associated with c1 : " + c1.getClassLoader());
		System.out.println("Class loader associated with c2 : " + c2.getClassLoader());
		System.out.println("Class loader associated with c3 : " + c3.getClassLoader());
	}
}

Output

Class loader associated with c1 : com.sun.tools.javac.launcher.Main$MemoryClassLoader@19dc67c2
Class loader associated with c2 : null
Class loader associated with c3 : null