简体   繁体   English

嵌入式Jetty(v9)和jetty-env.xml产生了奇怪的类加载器问题

[英]Embedded Jetty (v9) and jetty-env.xml create weird class loader issue

I'm programatically starting Jetty that points at a war. 我正在以编程方式启动指向战争的码头。 I also have a jetty-env.xml in the WEB-INF that defines a JNDI source. 我在WEB-INF中也有一个jetty-env.xml,它定义了JNDI源。 However, on startup, it gives me an error about class loader issues. 但是,启动时,它给我有关类加载器问题的错误。 I think because the main() creates one WebAppContext from java's AppClassLoader and the JNDI is in a WebAppContext defined in the jetty-env.xml. 我认为是因为main()从java的AppClassLoader创建一个WebAppContext,而JNDI在jetty-env.xml中定义的WebAppContext中。

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:/home/ckessel/Projects/exploded-war/WEB-INF/jetty-env.xml

I wouldn't think combining an embedded Jetty startup with an jetty-env.xml is that bizarre, but I've read through a ton of the eclipse Jetty docs on embedded Jetty and JNDI and so forth, but it doesn't seem to cover this combination. 我不认为将嵌入式Jetty初创公司与jetty-env.xml结合起来是很奇怪的,但是我已经阅读了很多关于嵌入式Jetty和JNDI的Eclipse Jetty文档,等等,但似乎并没有涵盖这个组合。

An ideas? 有想法吗?

The jetty-env.xml: jetty-env.xml:

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <New id="postgreSQL Datasource" class="org.eclipse.jetty.plus.jndi.Resource">
        <Arg></Arg>
        <Arg>jdbc/postgres</Arg>
        <Arg>
...postgres config stuff here...
        </Arg>
    </New>
</Configure>

The main()...the classlist stuff was stolen from http://www.eclipse.org/jetty/documentation/current/jndi-embedded.html main()...类列表的内容从http://www.eclipse.org/jetty/documentation/current/jndi-embedded.html被盗

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

        // Enable parsing of jndi-related parts of web.xml and jetty-env.xml
        Configuration.ClassList classlist = Configuration.ClassList.setServerDefault(server);
        classlist.addAfter(
                "org.eclipse.jetty.webapp.FragmentConfiguration",
                "org.eclipse.jetty.plus.webapp.EnvConfiguration",
                "org.eclipse.jetty.plus.webapp.PlusConfiguration");

        // Create the webapp for our war.
        WebAppContext webapp = new WebAppContext();
        webapp.setWar("/home/ckessel/Projects/build/exploded-war");
        webapp.setContextPath("/");
        server.setHandler(webapp);

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

2 things for you to do. 2件事情要做。

  1. Get rid of duplicate jars in your war's WEB-INF/lib directory. 消除战争的WEB-INF / lib目录中的重复jar。 Mainly the jetty-webapp.jar it shouldn't be there. 主要是jetty-webapp.jar它不应该在那里。

  2. If you are only ever going to run 1 webapp in this embedded jetty instance, then consider using WebAppContext.setParentLoaderPriority(true) , to make the ClassLoader behavior more akin to standard Java (as opposed to the reverse requirements for Servlets) 如果您只打算在此嵌入式码头实例中运行1个Webapp,则考虑使用WebAppContext.setParentLoaderPriority(true) ,以使ClassLoader行为更类似于标准Java(与Servlet的反向要求相反)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM