Every java program has at least three class loaders.
- The bootstrap class loader
- The extension class loader.
- The system class loader.
The bootstrap class loader loads the system classes from the JAR Fle rt.jar(part of JRE System Library) . It is an integral part of the virtual machine and is usually implemented in C. There is no Class loader object corresponding to the bootstrap class loader. For example.
String.class.getClassLoader() will return null since String class is loader by bootstrap loader.
The extension class loaders loads "standard extensions "from the jre/lib/ext directory.
The system class loader loads the application classes. It locates the classes in the directories and JAR/ZIP files on the class path.
The System class loader also called application class loader.
In Sun's java implementation , the extension adn system class loaders are implemented in java. Both are instances of URLClass loader.
You can just go through the source code of URLClass Loader and ClassLoader classes to understand more about the loading process.
protected synchronized Class loadClass(String name, boolean resolve)throws ClassNotFoundException
{
// First, check if the class has already been loaded
Class c = findLoadedClass(name);
if (c == null) {
try {
if (parent != null) {
c = parent.loadClass(name, false);
} else {
c = findBootstrapClass0(name);
}
} catch (ClassNotFoundException e) {
// If still not found, then invoke findClass in order to find the class.
c = findClass(name);
}
}
if (resolve) {
resolveClass(c);
}
return c;
}
This is the implementation of loadClass method in ClassLoader class.
- Whenever a call to this method, it will check whether this class is already loaded by calling 'findLoadedClass()' method>
- If the class is not already loaded and if there is a parent class loader , it will ask the parent to load the class.
- If the parent class is not available, it will ask the BootStrapClass loader to load the class.
- If both condtions are not satisfied and an ClassNotFoundException occurs , it will call a method to find and load the class.
Now we come to create a custom Class Loader.
To implement this we have the following process>
- A Class should extend the abstract class ClassLoader and override the method loadClass.
- As i said before we can write the code similar to the original loadClass method.
- Here there is another method called 'defineClass()' whose signature is
"defineClass(String name, byte[] b, int off, int len)"
This method is used whenever we have the class name and byte array of the class file. This is a native method means not written in java. It will return class object after loading the class.
- The bootstrap class loader
- The extension class loader.
- The system class loader.
The bootstrap class loader loads the system classes from the JAR Fle rt.jar(part of JRE System Library) . It is an integral part of the virtual machine and is usually implemented in C. There is no Class loader object corresponding to the bootstrap class loader. For example.
String.class.getClassLoade
The extension class loaders loads "standard extensions "from the jre/lib/ext directory.
The system class loader loads the application classes. It locates the classes in the directories and JAR/ZIP files on the class path.
The System class loader also called application class loader.
In Sun's java implementation , the extension adn system class loaders are implemented in java. Both are instances of URLClass loader.
You can just go through the source code of URLClass Loader and ClassLoader classes to understand more about the loading process.
protected synchronized Class loadClass(String name, boolean resolve)throws ClassNotFoundException
{
// First, check if the class has already been loaded
Class c = findLoadedClass(name);
if (c == null) {
try {
if (parent != null) {
c = parent.loadClass(name, false);
} else {
c = findBootstrapClass0(name);
}
} catch (ClassNotFoundException e) {
// If still not found, then invoke findClass in order to find the class.
c = findClass(name);
}
}
if (resolve) {
resolveClass(c);
}
return c;
}
This is the implementation of loadClass method in ClassLoader class.
- Whenever a call to this method, it will check whether this class is already loaded by calling 'findLoadedClass()' method>
- If the class is not already loaded and if there is a parent class loader , it will ask the parent to load the class.
- If the parent class is not available, it will ask the BootStrapClass loader to load the class.
- If both condtions are not satisfied and an ClassNotFoundException occurs , it will call a method to find and load the class.
Now we come to create a custom Class Loader.
To implement this we have the following process>
- A Class should extend the abstract class ClassLoader and override the method loadClass.
- As i said before we can write the code similar to the original loadClass method.
- Here there is another method called 'defineClass()' whose signature is
"defineClass(String name, byte[] b, int off, int len)"
This method is used whenever we have the class name and byte array of the class file. This is a native method means not written in java. It will return class object after loading the class.
nice writeup. for more details and eclipse options for class loading, see my complimentary article on class loading in java
ReplyDelete