In a few simple sentences, what is a Java ClassLoader, when is it used and why?
OK, I read a wiki article. ClassLoader loads classes. OK. So if I include jar files a
When you asking why does the ClassLoader class exist, the reason is pretty simple - it's the class responsible for finding and loading class files at run time.
Let's elaborate it.
In JVM, every Class is loaded by some instance of java.lang.ClassLoader
.
Whenever a new JVM is started by you usual Java program launching java <classname>
command, the first step is to load all the key classes in memory required for proper working like java.lang.Object
and other runtime classes (rt.jar
).
Now, there are actually 3 parts to ClassLoader:
The BootstrapClassLoader
is responsible for making these classes
available i.e. loading these classes in memory.
The next task is to load any external libraries / JARs into the
memory for proper working of the application. The ExtClassLoader
is
responsible for this task. This class loader is responsible for
loading all the .jar files mentioned in java.ext.dirs path.
The third and the main important class loader is the AppClassLoader
.
The application class loader is responsible for loading of the class
files mentioned in the java.class.path system property.
It's also important to note that the default ClassLoader implementations could be overridden enabling you to customize the JVM in useful and interesting ways, allowing you to completely redefine how class files are brought into the system.
Check it out to learn more about Java Class Loader.