我一直没有成功地尝试包含slf4j
在一个项目中。我已经将slf4j-api-1.7.32.jar
和添加slf4j-jdk14-1.7.32.jar
到类路径中,但无论我尝试过什么,我最终都会得到一个java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
.
错误的初始部分是
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at com.google.gwt.dev.shell.jetty.JettyLauncher$WebAppContextWithReload$WebAppClassLoaderExtension.findClass(JettyLauncher.java:354)
at org.mortbay.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:366)
at org.mortbay.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:337)
...
(如果有帮助,很高兴包含更多内容,但编译器会在那时将其切断)
该项目在 Eclipse 中,我已经尝试过
似乎没有任何效果。
然而,奇怪的是,使用完全相同的库集合,它在我编写的 JUnit 测试中运行良好。
今天早上,一位同事发现添加这对slf4j
库可以使war/WEB-INF/lib
事情正常工作,但这不是一个可行的长期解决方案。
我希望那里有人有一些聪明的想法!
编辑:取得了更多进展。从这里需要添加
<Set name="systemClasses">
<Array type="java.lang.String">
<!-- we from jetty WebAppContext source code ...-->
<Item>java.</Item>
<Item>javax.servlet.</Item>
<Item>javax.xml.</Item>
<Item>org.mortbay.</Item>
<Item>org.xml.</Item>
<Item>org.w3c.</Item>
<Item>org.apache.commons.logging.</Item>
<Item>org.apache.log4j.</Item>
<!-- and ... added slf4j -->
<Item>org.slf4j.</Item>
</Array>
</Set>
到jetty-web.xml
。现在需要找出部署脚本中需要更改的内容。
将以下“systemClasses”集添加到 jetty-web.xml
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
...
<Set name="systemClasses">
<Array type="java.lang.String">
<!-- we copied these paths from jetty WebAppContext source code ...-->
<Item>java.</Item>
<Item>javax.servlet.</Item>
<Item>javax.xml.</Item>
<Item>org.mortbay.</Item>
<Item>org.xml.</Item>
<Item>org.w3c.</Item>
<Item>org.apache.commons.logging.</Item>
<Item>org.apache.log4j.</Item>
<!-- and ... added slf4j -->
<Item>org.slf4j.</Item>
<!-- we must promote slf4j to system classes, otherwise gwt
hosted mode will not allow loading them due to a policy
that don't allow server classes to be loaded from the
outside world (see gwt JettyLauncher source code). -->
</Array>
</Set>
</Configure>
如 2009 年在此留言板上所见:https : //www.mail-archive.com/[email protected]/msg14754.html
逐步通过JettyLauncher.WebAppContextWithReload.WebAppClassLoaderExtension.findClass(String name)
显示,在第一次加载时,大多数人super.findClass(name)
抛出了一个ClassNotFoundException
,然后当他们通过时,isServerPath
他们开始使用systemClassLoader
来从 .jar 文件加载它们。现在systemClassLoader
has 父类型URLClassLoader
有一个字段ucp
( URLClassPath
) ,该字段包含path
可以从中加载类的所有 .jar 文件的列表(下)。看看这里,slf4j jars 确实存在,但由于某种原因isServerPath
返回 true。这就是最终导致找到上述解决方案的原因。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句