java - 嵌入式 Jetty (v9) 和 jetty-env.xml 创建奇怪的类加载器问题

标签 java jetty

我正在以编程方式启动指向 war 的 Jetty。我还在 WEB-INF 中定义了 JNDI 源的 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

我不认为将嵌入式 Jetty 启动与 jetty-env.xml 结合起来有那么奇怪,但我已经阅读了大量关于嵌入式 Jetty 和 JNDI 等的 eclipse Jetty 文档,但事实并非如此。似乎没有涵盖这种组合。

一个想法?

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>

main()...classlist 的内容被从 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 件事。

  1. 删除 war 的 WEB-INF/lib 目录中的重复 jar。 主要是 jetty-webapp.jar 它不应该在那里。

  2. 如果您只想在此嵌入式 jetty 实例中运行 1 个 Web 应用程序,请考虑使用 WebAppContext.setParentLoaderPriority(true) ,使 ClassLoader 行为更类似于标准 Java(而不是 Servlet 的相反要求)

关于java - 嵌入式 Jetty (v9) 和 jetty-env.xml 创建奇怪的类加载器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25435257/

相关文章:

java - 我的绘画方法给了我一个错误,我不知道为什么

java - WebSocket握手时出错: Unexpected response code: 404 WrappedWebSocket @ VM222:161

plugins - 如何查看Gradle Jetty插件中使用的Jetty版本

jetty - 如何在 xsbt-web-plugin 上设置 jetty 的 contextPath?

maven - 如何告诉jetty-maven-plugin运行哪个版本的jetty?

java - GSON - 从字符串中获取 JSON 值

Java JDBC ActiveDirectoryAuthentication Azure SQLServer 失败并出现 401/adfs/services/trust/13/windowstransport 异常

java - 如何使用Hibernate4Module

java - 3gb后下载失败

java - Jetty RESTful 具有多种数据格式