Saturday, July 5, 2008

Java & class loaders

Most important reasons of having different class loader are security, abstraction and separating out functionality in different layers.

How?

There are two main function of class loader

1. Find out the byte code of particular class and this class loader can use its own method to find it out. It can load them from the local disk, fetch them across the net using any protocol, or it can just create the byte code on the spot.

2. Class loaders define the namespaces seen by different classes and how those namespaces relate to each other.

There are two important varieties of class loader, one is primordial class loader and second is class loader objects.

There is only one primordial class loader which is essential part of java VM. The primordial class loader is involved in the bootstrapping the java environment.

The primordial class loader uses native operating system file access capabilities to open and read java class files from disk to into byte arrays.

This provides java with the ability to bootstrap itself and provide essential function.

Now consider the second variety, Class loader object, what it does is, it load the classes that are not needed to bootstrap the VM into running java environment.

The class loaded by this type of class loader is untrusted by default.

What Java achieve by differentiating class loaders?

What Java achieve by differentiating class loader is, due to primordial class loader, they hides the complexity of handling native operating system calls, ability to bootstrap itself and due class loader objects, java is capable of loading the classes that are not needed to bootstrap VM.

Java secures the bootstrap process by separating the class loader.

Java segregates the functionality in different layer, what they achieving in layering is any unaccepted error / exception is handled is at upper layer.

No comments: