Java Custom Class Loaders 
by Brian Roelofs

Listing One
-----------------------------------------------------------
java.lang.ClassNotFoundException: mypackage.SimpleSquare
java.lang.ClassNotFoundException: mypackage.DeviceController
Class java.awt.Canvas loaded by the system loader
Class mypackage.SimpleSquare loaded by CustomClassLoader
Class java.lang.Object loaded by the system loader
Class DeviceControls loaded by the system loader
Class mypackage.DeviceController loaded by CustomClassLoader
Class java.lang.Throwable loaded by the system loader
Class java.awt.Rectangle loaded by the system loader
Class java.awt.Component loaded by the system loader
Class java.awt.Color loaded by the system loader
Class java.lang.System loaded by the system loader
Class java.io.PrintStream loaded by the system loader
Device is now on
Getting device power
Battery Power = 10
Calling onOff() and getBatterPower() using reflection
Device is now on
Getting device power
-----------------------------------------------------------
java.lang.ClassNotFoundException: mypackage.SimpleSquare
java.lang.ClassNotFoundException: mypackage.DeviceController
Loading class mypackage.SimpleSquare
Loading class java.awt.Canvas
Class mypackage.SimpleSquare loaded by CustomLoader2
Loading class mypackage.DeviceController
Loading class java.lang.Object
Loading class DeviceControls
Class mypackage.DeviceController loaded by CustomLoader2
Loading class java.lang.Throwable
Loading class java.awt.Rectangle
Loading class java.awt.Component
Loading class java.awt.Color
Loading class java.lang.System
Loading class java.io.PrintStream
Device is now on
Getting device power
Battery Power = 10
Calling onOff() and getBatterPower() using reflection
Device is now on
Getting device power

Listing Two
Class     loadedClass  =  customLoader.loadClass("MyClass");
MyClass   vmClass = new MyClass();  // MyClass loaded by Java 
boolean   theSame = vmClass.isInstance(loadedClass);
System.out.println("Are the classes the same? " + theSame);

Listing Three
package mypackage;
import java.awt.*;
/* A real simple Canvas class to test with our class loader.
SimpleSquare appears as a red square. */
public class SimpleSquare extends java.awt.Canvas
{
    public SimpleSquare()
    {
        this.setBounds( new Rectangle( 10, 30, 50, 50) );
        this.setBackground(Color.red);
    }
}



2


