SECTION: 250-Configuration TITLE: 200classloading QUESTION: I'm having class loading problems?
Class loading in a web container is slightly more complex than a normal java application.
The normal configuration is for each web context (webapplication or war file) is given it's own classloader, which has the system classloader as it's parent. Such a classloader hierarchy is normal in Java, however the servlet specification complicates the hierarchy by requiring that:
WEB-INF/lib
or WEB-INF/classes
have
priority over classes on the parent class loader. This is the opposite of the normal behaviour
of a java 2 class loader.WEB-INF/lib
or WEB-INF/classes
. Unfortunately the specification does
not clearly state what classes are "System" classes and it is unclear if all javax classes
should be treated as System classes.
Jetty provides configuration options to control all three of these options. The method
org.mortbay.http.HttpContext.setClassLoaderJava2Compliant(boolean)
allows
the normal java 2 behaviour to be used and all classes will be loaded from the system
classpath if possible. This is very useful if the libraries that a web application uses
are having problems loading classes that are both in a web application and on the
system classpath.
The methods setSystemClasses(String[])
and setServerClasses(String[])
may be called on either the org.mortbay.http.HttpServer
or org.mortbay.http.HttpContext
class to configure the whole server or just a particular context. This allows fine control
over what classes can be seen or overridden by a web application. Absolute classname can be passed, names
ending with . are treated as packages names and names starting with - are treated as negative matches.
These configuration may be setup either in code, in jetty.xml
or in a jetty-web.xml
file. An example for jetty.xml
is below:
<Call name="addWebApplication"> <Arg>/mywebapp</Arg> <Arg>./webapps/mywebapp.war</Arg> <Set name="classLoaderJava2Compliant">true</Set> <!-- System classes cannot be overriden by a HttpContext or webapp --> <Set name="systemClasses"> <Array type="java.lang.String"> <Item>java.</Item> <Item>javax.servlet.</Item> <Item>javax.xml.</Item> <Item>org.mortbay.</Item> <Item>org.xml.</Item> <Item>org.w3c.</Item> </Array> </Set> <!-- Server classes are hidden from a HttpContext or webapp --> <Set name="serverClasses"> <Array type="java.lang.String"> <Item>-org.mortbay.http.PathMap</Item> <Item>-org.mortbay.</Item> <Item>org.mortbay.start.</Item> <Item>org.mortbay.stop.</Item> <Item>org.mortbay.jetty.Server</Item> </Array> </Set> </Call>