gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:37:42 28 4
gpt4 key购买 nike

我正在以编程方式启动指向 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com