SECTION: 250-Configuration TITLE: 200classloading QUESTION: I'm having class loading problems? <p> Class loading in a web container is slightly more complex than a normal java application. </p><p> 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:</p> <ul> <li>Classes contained within <code>WEB-INF/lib</code> or <code>WEB-INF/classes</code> have priority over classes on the parent class loader. This is the opposite of the normal behaviour of a java 2 class loader.</li> <li>System classes such as java.lang.String may not be replaced by classes in <code>WEB-INF/lib</code> or <code>WEB-INF/classes</code>. 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.</li> <li>Server implementation classes should be hidden from the web application and should not be available in any class loader. Unfortunately the specification does not state what is a Server class and it is unclear if common libraries like the xerces parser should be treated as Implementation classes. </ul> <p> Jetty provides configuration options to control all three of these options. The method <code>org.mortbay.http.HttpContext.setClassLoaderJava2Compliant(boolean)</code> 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. </p> <p>The methods <code>setSystemClasses(String[])</code> and <code>setServerClasses(String[])</code> may be called on either the <code>org.mortbay.http.HttpServer</code> or <code>org.mortbay.http.HttpContext</code> 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. </p> <p> These configuration may be setup either in code, in <code>jetty.xml</code> or in a <code>jetty-web.xml</code> file. An example for <code>jetty.xml</code> is below: <pre> <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> </pre>