Search code examples
web-servicesjetty

External jar available to all Jetty web services


I have added the external jar I want to {jetty.home}/lib/ext but when I rebuild my war file without this jar, deploy it, and restart the web service, it isn't able to find the jar and I'm getting:

java.lang.NoClassDefFoundError

I'm not sure I'm headed down the right path, but essentially I would like to remove a jar from the war file of multiple web services, and have them all reference the jar via classpath (so that it can be easily updated without rebuilding and deploying the war file.

I've tried in /lib/ext, /lib/, and /resources.

[root]$ java -jar start.jar --version
Active Options: [Server, jmx, resources, websocket]
Version Information on 15 entries in the classpath.
Note: order presented here is how they would appear on the classpath.
  changes to the OPTIONS=[option,option,...] command line option will be reflected here.
 0:      7.0.1.v20091125 | ${jetty.home}/lib/jetty-xml-7.0.1.v20091125.jar
 1:  2.5.0.v200806031605 | ${jetty.home}/lib/servlet-api-2.5.jar
 2:      7.0.1.v20091125 | ${jetty.home}/lib/jetty-http-7.0.1.v20091125.jar
 3:      7.0.1.v20091125 | ${jetty.home}/lib/jetty-continuation-7.0.1.v20091125.jar
 4:      7.0.1.v20091125 | ${jetty.home}/lib/jetty-server-7.0.1.v20091125.jar
 5:      7.0.1.v20091125 | ${jetty.home}/lib/jetty-security-7.0.1.v20091125.jar
 6:      7.0.1.v20091125 | ${jetty.home}/lib/jetty-servlet-7.0.1.v20091125.jar
 7:      7.0.1.v20091125 | ${jetty.home}/lib/jetty-webapp-7.0.1.v20091125.jar
 8:      7.0.1.v20091125 | ${jetty.home}/lib/jetty-deploy-7.0.1.v20091125.jar
 9:      7.0.1.v20091125 | ${jetty.home}/lib/jetty-servlets-7.0.1.v20091125.jar
10:      7.0.1.v20091125 | ${jetty.home}/lib/jetty-jmx-7.0.1.v20091125.jar
11:                (dir) | ${jetty.home}/resources
12:      7.0.1.v20091125 | ${jetty.home}/lib/jetty-websocket-7.0.1.v20091125.jar
13:      7.0.1.v20091125 | ${jetty.home}/lib/jetty-util-7.0.1.v20091125.jar
14:      7.0.1.v20091125 | ${jetty.home}/lib/jetty-io-7.0.1.v20091125.jar

Solution

  • Jetty 7.0.1 is very old now. Consider upgrading. (9.2.5.v20141112 is the current stable version)

    Alright, back to your issue. You don't have the ext option enabled.

    Edit your ${jetty.home}/start.ini and add ext to the end of the line that starts with OPTIONS=

    Know that all this does is makes any jars that you put there present on the server classpath, suitable for the server to use for things like JNDI resources (such as database DataSource references).

    The mere existence of a jar in ${jetty.home}/lib/ext does not mean its accessible by your webapps. The WebApp ClassLoader isolation (a servlet spec requirement) prevents this.

    But all is not lost, Jetty provides means to configure the WebApp ClassLoader to "poke holes" into this isolation layer, allowing for control over individual classes and/or entire package namespaces behavior.

    This is done by setting up and configuring the WebAppContext in a Jetty Context XML in your ${jetty.home}/webapps/ directory.

    You'll have 2 things you can configure.

    addSystemClass(String)

    This is a class (or package) that cannot be replaced by the web application, and they are always loaded via the system classloader

    addServerClass(String)

    These are classes that are hidden from being loaded by the web application using the system classloader, so if a webapplication needs to load any of such classes, it has to include them in its distribution.


    So, lets say you add a file called ${jetty.home}/lib/ext/corpcommon.jar, and it has a package namespace of com.corp.common, your resulting XML files will contain following snippets

    <Configure class="org.eclipse.jetty.webapp.WebAppContext">
     ...
     <!-- webapp cannot override these, use the server version always,
          even if the webapp has its own copy -->
     <Call name="addSystemClass">
       <Arg>com.corp.common.</Arg>
     </Call>
     <!-- expose com.corp.common to webapp -->
     <Call name="addServerClass">
       <Arg>-com.corp.common.</Arg>
     </Call>