java - 程序运行时未在类路径中包含 slf4j
问题描述
我一直没有成功地尝试包含slf4j
在一个项目中。我已经将slf4j-api-1.7.32.jar
and添加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 中,我已经尝试过
- 将库放在类路径的开头
- 把它们放在最后
- 关闭和打开 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/google-web-toolkit@googlegroups.com/msg14754.html
我们如何找到解决方案:
虽然逐步JettyLauncher.WebAppContextWithReload.WebAppClassLoaderExtension.findClass(String name)
显示,在第一次加载时,大多数人都super.findClass(name)
扔了一个ClassNotFoundException
,然后当他们通过isServerPath
时,他们开始使用systemClassLoader
.jar 文件来加载它们。现在systemClassLoader
has 父类型URLClassLoader
有一个字段ucp
( ),其中包含所有可以从中加载类的 .jar 文件URLClassPath
的列表(在 下)。path
看看这里,slf4j jar 确实存在,但由于某种原因isServerPath
返回了 true。这最终是导致找到上述解决方案的原因。
推荐阅读
- bash - 变量中 bash 命令的输出包含在撇号中。为什么?
- android - Flutter IntelliJ 插件中没有控制台输出?
- powershell - 管道子项目
- roku - 水平对齐为“右”在明亮的脚本中不起作用
- mongodb - Mongoose - 填充包含其他字段的对象
- python - Kivy - 无法在 Ubuntu 上打开设备“/dev/input/event7”,没有权限
- react-hooks - React - API Call to set mutliple state variables (Better Alternatives)
- prestashop - 将产品添加到购物车的 Prestashop 问题
- python - django rest 框架 - 向后序列化以避免 prefetch_related
- python - 绘制图像垂直投影的直方图