0

I'm trying to run jetty in embedded mode. It appears to be ignoring all the classes bundled in my war file, whether under WFB-INF/classes or WEB-INF/lib.

My startup code:

package rfd;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.xml.XmlConfiguration;

public class RfdWar
{
  public static void main(String[] args) throws Exception
  {
    System.setProperty("org.eclipse.jetty.LEVEL", "DEBUG");
    Server server = new Server(8080);

    org.eclipse.jetty.webapp.Configuration.ClassList classlist = org.eclipse.jetty.webapp.Configuration.ClassList.setServerDefault(server);
    classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration", "org.eclipse.jetty.plus.webapp.PlusConfiguration");

    Resource jettyEnv = Resource.newSystemResource("jetty-env.xml");
    XmlConfiguration conf = new XmlConfiguration(jettyEnv.getInputStream());
    Object obj = conf.configure();
    WebAppContext context = (WebAppContext)obj;

    context.setWar("/tmp/thewar.war");
    context.setContextPath("/");
    context.setParentLoaderPriority(true);

    server.setHandler(context);

    server.start();
    server.join();
  }
}

My command line:

export JETTYHOME=<my_jetty_home>
JHL=$JETTYHOME/lib

export CLASSPATH=.:$JHL/jetty-server-9.2.10.v20150310.jar:$JHL/jetty-util-9.2.10.v20150310.jar:$JHL/jetty-http-9.2.10.v20150310.jar:$JHL/servlet-api-3.1.jar:$JHL/jetty-io-9.2.10.v20150310.jar:$JHL/jetty-webapp-9.2.10.v20150310.jar:$JHL/jetty-servlet-9.2.10.v20150310.jar:$JHL/jetty-security-9.2.10.v20150310.jar:$JHL/jetty-xml-9.2.10.v20150310.jar:$JHL/jetty-plus-9.2.10.v20150310.jar:$JHL/jetty-jndi-9.2.10.v20150310.jar:$JHL/jsp/javax.servlet.jsp-2.3.2.jar:$JHL/jsp/javax.servlet.jsp-api-2.3.1.jar

java rfd.RfdWar

The server does launch correctly and definitely reads web.xml packaged in the war. But when I try accessing the URL I'm getting an error that my class, that's packaged with the war, is missing.

Is there anything else I need to do to tell jetty to honor the classes packaged in the war?

Ya.
  • 1,671
  • 4
  • 27
  • 53

1 Answers1

2

This command ...

context.setParentLoaderPriority(true);

Basically says that when the webapp is attempting to load a class, the server classpath is checked first, then the webapp's classpath.

So if you happen to have the same classes in both places, the server version will be used, ignoring the one on the webapp.

Set this to false to get honest servlet spec behavior, with all of the WebApp classloader isolation.

Also, this is wrong, in many different ways.

Resource jettyEnv = Resource.newSystemResource("jetty-env.xml");
XmlConfiguration conf = new XmlConfiguration(jettyEnv.getInputStream());
Object obj = conf.configure();
WebAppContext context = (WebAppContext)obj;

The proper way to get jetty-env.xml to be loaded is to use the establish the WebApp and ClassList Configuration that performs this function from within the appropriate classloader and thread scope.

See documentation at https://www.eclipse.org/jetty/documentation/current/jndi-embedded.html

And prior answer at Can't get jetty to read jetty-env.xml

Community
  • 1
  • 1
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136