application class loader code example

Example 1: custom classloader

package test.main;
 
import java.lang.reflect.InvocationTargetException;
 
public class Test {
 
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, Exception {
 
		MyClassLoader loader = new MyClassLoader(
	            Test.class.getClassLoader());
		
		System.out.println("loader name---- " +loader.getParent().getClass().getName());
		
		//This Loads the Class we must always
		//provide binary name of the class
		Class<?> clazz =
	            loader.loadClass("test.frugalis.Frugalis");
		
        System.out.println("Loaded class name: " + clazz.getName());
 
        //Create instance Of the Class and invoke the particular method
        Object instance = clazz.newInstance();
        
        clazz.getMethod("printMyName").invoke(instance);
	}
}

Example 2: custom classloader

loader name---- sun.misc.Launcher$AppClassLoader
Class Loading Started for test.frugalis.Frugalis
*********Inside getClass*********
Name of Filetest\frugalis\Frugalis.class
<<<<<<<<<Inside loadClassData>>>>>>
Size of byte array 519
Class Loading Started for java.lang.Object
Loaded class name: test.frugalis.Frugalis
Class Loading Started for java.lang.System
Class Loading Started for java.io.PrintStream
Welcome Frugalis Minds

Example 3: custom classloader

package test.frugalis;
 
public class Frugalis {
 
	public void printMyName(){
		System.out.println("Welcome Frugalis Minds");
	}
}

Tags:

Misc Example