Search code examples
web-applicationsjettyclassloader

Failed to configure Webapp Classloading on Jetty 9 and set System Classes


I'm trying to deploy a web application that extends the class: org.eclipse.jetty.proxy.ProxyServlet.

In the start.ini file, I set:

OPTIONS=Server,client,proxy

and I verified that jetty-distribution-9.0.3.v20130506/lib/jetty-proxy-9.0.3.v20130506.jar file is actually included into the classpath, by using the command:

java -jar start.jar --dry-run

When I try to start the server, I still get the following exception:

java.lang.ClassNotFoundException: org.eclipse.jetty.proxy.ProxyServlet
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:420)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:382)
[...]

So I searched through the documentation, and from the section Jetty Classloading I realized I have to configure the WebAppContext, in order to unset ProxyServlet as a Server Class (as server classes are hidden to web apps). Hence I modified the jetty-web.xml file by adding the following lines:

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
 <Call name="prependServerClass"><Arg>-org.eclipse.jetty.proxy.</Arg></Call>
</Configure>

But it didn't work! Then I read this:

You can configure webapp classloading by several methods on the WebAppContext. You can call these methods directly if you are working with the Jetty API, or you can inject methods from a context XML file if you are using the Context Provider (???). You CANNOT set these methods from a jetty-web.xml file, as it executes after the classloader configuration is set.

What the heck does that mean?? I'm not configuring the application within the code, but by XML files only. So if it's not in the jetty-web.xml file, where should I put the WebAppContext configuration? What are the path, file name and XML sections I have to modify, precisely?


Solution

  • Lots going on here. First off, typical usage... normally people that use the ProxyServlet simply put jetty-proxy and its dependencies into their war file and configure it as any other servlet in their web.xml.

    Next..you mention a jetty-web.xml file. If that is the xml file that is inside of your webapp then that is processed after the webapp context has basically been configured, so it is too late to the party to be able to influence the classloader configuration of the webapp context. The file you would need to edit to tweak server classes for the webapp context is the context xml file for the webapp you are deploying. In other words...this type:

    http://www.eclipse.org/jetty/documentation/current/quickstart-config-what.html#intro-jetty-configuration-contexts

    Those context files are typically located in the jetty.home/webapps directory of jetty 9, and previously in the jetty.home/contexts directory in 6, 7, and 8. If you are using the distribution look for the text.xml file in the webapps dir, that is an example used for the test webapp.