Java ClassLoader 2

Inheritance system

When JVM start, bookstrap class loader will be loaded into java core.
like class in rt.jar.bookstrap is other ClassLoader’s parent,
it is the only loader has no parent.

Then the extension class loader, it’s parent is bookstrap,
it used to load class from java’s environment path java.ext.dir.

Third the most important class loader, the system classpath
class loader, developer’s self define class.
It’s parent is extension, it load class from
java’s environment path java.class.path.
Path can be set in -classpath parameter in command.

Tips

The system is not Inheritance but Delegation
Most loader will find resource in it’s parents, if not find in local.
Actually it ensure the high level loader’s class
can be assign with lower level loader’s type.

The motivation of Delegation is avoid load same class multi times.
When in 1995, java be used in Applet.
At that time the network bandwidth has priority, so program load class
when class be used then load it. Model is load parent first.

But in this time java show it’s power in server part.
Server need loader follow reverse delegation priciple,
to load local class first, if can’t load class in parent.

JavaEE Delegation model

Tomcat class load sequence(delegation model)

  1. Bookstrap @rt.jar
  2. Extension @lib/ext
  3. System @%catalina_home%/bin/bootstrap.jar
  4. Common @%catalina_home%/lib
  5. Webapp @WEB-INF/lib

In tomcat default model,
try to load class in Bootstrap and Extension first.
If can’t load, try to load class in Webapp
If still can’t load, load in Common.

In Ali’s tomcat, it is in delegate model, so parent has priority.
So use Common loader first, then Webapp loader.