嵌入式码头忽略了我在战争中打包的类

时间:2015-03-27 16:57:52

标签: jetty

我正试图在嵌入模式下运行jetty。它似乎忽略了我的war文件中捆绑的所有类,无论是在WFB-INF / classes还是WEB-INF / lib下。

我的启动代码:

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();
  }
}

我的命令行:

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

服务器确实正确启动,并且肯定会读取战争中打包的web.xml。但是当我尝试访问该URL时,我收到的错误是我的课程与战争一起打包了。

我还需要做些什么才能告诉码头尊重战争中包装的课程吗?

1 个答案:

答案 0 :(得分:2)

这个命令......

context.setParentLoaderPriority(true);

基本上说当webapp尝试加载类时,首先检查服务器类路径,然后检查webapp的类路径。

因此,如果您碰巧在两个地方都有相同的类,将使用服务器版本,忽略webapp上的版本。

将此设置为false以获得诚实的servlet规范行为,并将所有WebApp类加载器隔离。

此外,这在很多方面都是错误的。

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

获取jetty-env.xml的正确方法是使用在相应的类加载器和线程范围内建立执行此功能的WebApp和ClassList配置。

请参阅https://www.eclipse.org/jetty/documentation/current/jndi-embedded.html

上的文档

之前在Can't get jetty to read jetty-env.xml

回答
相关问题