Tomcat-ClassLoader

JVM class loader

JVM contain loaders:

  1. BootStrapClassLoader
    jvm need foundation class
  2. ExtClassLoader
    load $JAVA_HOME$/jre/lib/ext jars
  3. AppClassLoader
    $CLASS_PATH$ and -classpath, and class not be contained above
  4. CustomClassLoader
    self define class loader[progress]

different classloader’s class can’t use in mix,
even if the class is same package name and same class name.

JVM use delegation model

  1. pass request to father until the top
  2. if top can’t load return to sub level
  3. if the floor can’t load class throw ClassNotFoundException

java class loader

So if a same path class be put in $CLASS_PATH$ directory, java will load
$CLASS_PATH$ class first.

Tomcat class loader

Tomcat loader like this:

  1. Bootstrap
    guilde class loader $JAVA_HOME$/jre/lib/ext jars
  2. System
    tomcat container loading $CATALINA_HOME$/bin jars
  3. Common
    app common use class $CATALINA_HOME$/lib jars
  4. WebApp
    WEB-INF/classes and WEB-INF/lib developer’s workspace

Problem

When developing throw an exception from java.io.ObjectStreamClass

row:2034

1
2
3
4
5
6
7
throw new ClassCastException(
"cannot assign instance of " +
val.getClass().getName() + " to field " +
f.getDeclaringClass().getName() + "." +
f.getName() + " of type " +
f.getType().getName() + " in instance of " +
obj.getClass().getName());

From inspect the class type is a blank type only a path name.
Manually change the Class type suppress exception then jump breakpoint
the throw a exception like this:

Key Point

The Class used to communication is for other application use,
but the class contain a property that is from acceptor’s offical workspace.

Two application use a same tomcat and
put the dependent jar in $CATALINA_HOME$/lib.

It lead to a problem, the acceptor’s application load object and it’s field
class in two different classloader. one is WebAppClassLoader, on is bootstrap

  1. In object receive acceptor object use the common class jar[find in floor level]
  2. When object comes the property field’s class be found in workspace
    but object’s classloader can’t see,
    so as it’s sign target field class is just a shell.
  3. When suppress exception and jump breakpoint,
    the business class violate the loader constraint.