Chin
Chin

Reputation: 20675

java.lang.IllegalArgumentException: Object of class 'org.eclipse.jetty.webapp.WebAppContext' is not of type 'org.eclipse.jetty.webapp.WebAppContext'

I am trying to add extra classpaths to my web project. To do so I have followed this guide and created a jetty-env.xml file with the following content

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <Set name="extraClasspath">lib/ivy/</Set>     
</Configure>

and put it in the WEB-INF/ folder. However when I start Jetty I get the following exception:

2017-04-13 15:55:41.937:WARN:oejw.WebAppContext:main: Failed startup of context o.e.j.w.WebAppContext@51931956{/server,file:/C:/Users/foo/project/webapps/server/,STARTING}{C:\Users\foo\project\webapps\server}
java.lang.IllegalArgumentException: Object of class 'org.eclipse.jetty.webapp.WebAppContext' is not of type 'org.eclipse.jetty.webapp.WebAppContext'. Object Class and type Class are from different loaders. in file:/C:/Users/foo/project/webapps/server/WEB-INF/jetty-env.xml
    at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:296)
    at org.eclipse.jetty.xml.XmlConfiguration.configure(XmlConfiguration.java:248)
    at org.eclipse.jetty.plus.webapp.EnvConfiguration.configure(EnvConfiguration.java:124)
    at org.eclipse.jetty.webapp.WebAppContext.configure(WebAppContext.java:479)
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1337)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:741)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:505)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.deploy.bindings.StandardStarter.processBinding(StandardStarter.java:41)
    at org.eclipse.jetty.deploy.AppLifeCycle.runBindings(AppLifeCycle.java:186)
    at org.eclipse.jetty.deploy.DeploymentManager.requestAppGoal(DeploymentManager.java:498)
    at org.eclipse.jetty.deploy.DeploymentManager.addApp(DeploymentManager.java:146)
    at org.eclipse.jetty.deploy.providers.ScanningAppProvider.fileAdded(ScanningAppProvider.java:180)
    at org.eclipse.jetty.deploy.providers.WebAppProvider.fileAdded(WebAppProvider.java:440)
    at org.eclipse.jetty.deploy.providers.ScanningAppProvider$1.fileAdded(ScanningAppProvider.java:64)
...
    at org.eclipse.jetty.start.Main.main(Main.java:112)

What did I do wrong and how can I fix it?

Upvotes: 1

Views: 3551

Answers (2)

Chin
Chin

Reputation: 20675

I figured this out. I had a jetty-all jar file in the WEB-INF/lib folder. Removing it fixed the problem.

Upvotes: 2

Joakim Erdfelt
Joakim Erdfelt

Reputation: 49462

You cannot add extra classpath entries from within the WebApp startup (its too late, the WebAppClassloader already exists by that point).

That has to be defined outside of the WebAppContext deployment so that the startup of the WebApp can use it.

You'll need a XML deployable.

Assuming your war is ${jetty.base}/webapps/myserver.war

Create ${jetty.base}/webapps/myserver.xml

<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
                           "http://www.eclipse.org/jetty/configure_9_3.dtd">

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/myserver</Set>
  <Set name="war"><Property name="jetty.webapps" default="."/>/myserver.war</Set>
  <Set name="extraClasspath">/full/path/to/my/extra/lib/ivy/</Set>
</Configure>

Also note: extraClasspath cannot be a relative path. It has to be an absolute path.

If you have an expanded directory webapp deployment, consider a similar approach.

Assuming you have a webapp at ${jetty.base}/webapps/foo/

Create ${jetty.base}/webapps/foo.xml file

<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
                           "http://www.eclipse.org/jetty/configure_9_3.dtd">

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/foo</Set>
  <Set name="war"><Property name="jetty.webapps" default="."/>/foo/</Set>
  <Set name="extraClasspath">/full/path/to/my/extra/lib/ivy/</Set>
</Configure>

Upvotes: 1

Related Questions