If you have any doubts in the below, contact us by dropping a mail to the Kung Fu Panda.
We will get back to you very soon.
classloader is a part of JRE which loads the classes in the Java Virtual Machine.
each classloader in Java extends java.lang.ClassLoader.
You can write your own classloader by extending java.lang.ClassLoader to customize the way in which classloads loads the classes.
Theoretically, It is possible for one class to be loaded by two different classloaders, but normally its not done, because a classloader checks if the class is already loaded in some other classloader.
The parent classloader of any classloader is the classloader which loaded that classloader class.
Normally a class Foo is loaded only when it is needed in a class Bar in the following two ways
// when a new object is created.
Foo foo = new Foo();
// some static reference is made to the Foo for the first time.
In the above cases, both classes Foo and Bar are loaded by the same classloader.
Types of ClassLoaders
also called Primordial Classloader.
is the root classloader(parent of all classloaders) and does not have a parent.
written in native c/c++ code.
loads the class files present in jar files present in $JAVA_HOME/jre/lib/
considered the child of bootstrap classloader.
written and implemented in java, and uses Bootstrap classloader to launch itself up.
loads the class files present in the extensions director $JAVA_HOME/jre/lib/ext
it also loads any other classes whose path is specified by "java.ext.dirs" system property.
considered the child of Extension classloader.
written and implemented in java.
loads the class files present in java classpath("java.class.path" system variable).
Classloaders in J2EE application servers
J2EE application servers have their own custom classloaders.
since multiple applications can run simultaneously on the application servers, they have one classloader per application.
this helps in segregating the classes which are loaded by each application, and deploying/undeploying an application does not affect the other applications running on the server.
however, its better to restart the server, instead of separately deploying/undeploying the applications because some server like tomcat give memory leak warning on undeploying.
in case you read to read class from some place other than file, like from a website or over the network.
in case you need to customize the existing class, like wrap a class with AOP, like in case of Spring.
classloader should check things like package of the class loaded and should have some restriction on the package name, like it should not start with "java." or should start with "com.mycustomcll" etc.
classloader should also check that the class specified is valid and is not an attempt to compromise the system by passing a class name like "../../../tmp.." etc
one can write a custom classloader which encypts all the files on disk so that a decompiler cannot decompile it, and a password should be provided to decompile it.
Principles of ClassLoading
a classloader should first see if its parent can load the class, if yes, it should use it.
Only if the parent cannot load the class, should the classloader try to load the class itself.
A classloader can see/refer any class which its parent has loaded, but not any class which its children classloaders have loaded.
A class(identified by its fully qualified name) must be loaded by only one classloader.
If a class has been loaded by a classloader, it will not be loaded by its child classloaders.
NoClassDefFoundError Vs ClassNotFoundException Vs NoSuchMethodError
comes when you try to load a class dynamically during runtime, by doing something like 'Class.forName("foo.bar")', and foo.bar class is not found at runtime.
sometimes database connection classes are loaded using this way, Class.forName("com.mysql.....JDBCDriver");
is not a very serious error, so classified as an Exception
It is not serious because the application can recover from it, like if a program does Class.forName(DBDriver1) for multiple jdbc drivers, and uses the class, whichever is found.
comes when the class was available during compile time, but was not present when it is referred at runtime.
is serious so classified as 'Error' and not an 'Exception'.
can come in cases like in maven some of the dependencies are specified as 'provided', so that they are only present at compile time, and during runtime, they are supposed to be provided by the container.
comes when an application was compiled with a different version of a jar file, but a different version is present at runtime.
since the offending class is present at both compile time and run time, but with different versions, the call fails at runtime when a function is missing in the runtime jar.
Steps of Class Loading for a classloader(Simplified)
A classloader should the following steps to load a class.
first check whether it has the bytecode of the class being requested.
if yes, return the bytecodes of that class.
if no, check whether its parent classloader has the bytecode of the class being requested.
if the parent classloader has the bytecodes, return the bytecodes.
resolve the class if required, ie load its dependencies also.
if the parent classloader does not have the bytecodes, throw ClassNotFoundException.
Methods in java.lang.classLoader
// loads the class given the name of the class.
public Class loadClass(String name);
//creates a object of a Class, if you are given a byte array.
protected Class defineClass(byte bytes);
// returns the parent classloader
public ClassLoader getParent();
public URL getResource(String name);
running a class with verbose option for class prints which classes are being loaded. It is done by running a class using "java -verbose:class MyClass"
when a java application is started, the first class to start is the one whose main() method is called. As it refers other classes, they are loaded by the class loader.