Java ClassLoader 3

NoClassDefFoundError

When JavaEE load big quantity package in develop environment,
mantain dependency will become hard.
Especially JavaEE have alot of ClassLoader now.

When developing in IDE, package can be found, but can’t found class in deploy status.

1
2
3
4
5
6
7
@WebServlet(name = "NoClassDefFoundErrorServlet", urlPatterns = "/noClassDefFoundError.do")
public class NoClassDefFoundErrorServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.getWriter().println(TestCase.class.toString());
}
}

When TestCase.class from junit package, it’s scope is provider.
Junit package will not be depolyed,
so loader can’t find class in WEB-INF/lib.

NoSuchMethodError

This Error mean project included package version wrong.
The resource code use the high version package in IDE,
but package low level package in deploy environment.

Use -verbose:class to judge which version package jvm loaded.

ClassCastException

When more than one class loader loaded same class,
and use them to do operation, this problem may happen.
Because delegate model is different.

when in tradition delegate model this problem will not happen,
in JavaEE environment, module load it’s self resource first.
Although direct point outside resource, but use it’s parent loader’s resource first.

In JVM, how make sure a implement is the class’s implement?
JVM find a implement by ClassLoader + class full name.
class be define in a data structure named SystemDictionary.
This structure accept a ClassLoader and a name as parameters,
and return a class’s implement in call.

LinkageError

Different ClassLoader’s class, the method be overrided.
LinkageError is essentially same with LinkageError.

IllegalAccessError

The implement has no privilege to access method or field.
When class be loaded in a Unanticipated package, this problem happens

Summary

can’t find not right more than one
ClassNotFoundException NoClassDefFoundError IncompatibleClassChangeError NoSuchMethodError NoSuchFieldError IllegalAccessError ClassCastException LinkageError
Ctrl+Shift+T -verbose:class -verbose:class