程序运行时未选择在类路径中包含 slf4j

定制牧师

我一直没有成功地尝试包含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 中,我已经尝试过

  • 将库放在类路径的开头
  • 把它们放在最后
  • 关闭和打开 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 文件加载它们。现在systemClassLoaderhas 父类型URLClassLoader有一个字段ucp( URLClassPath) ,该字段包含path可以从中加载类的所有 .jar 文件的列表(下)。看看这里,slf4j jars 确实存在,但由于某种原因isServerPath返回 true。这就是最终导致找到上述解决方案的原因。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Groovy @ Slf4j批注:“类路径包含多个SLF4J绑定”

Maven SLF4J:类路径包含多个SLF4J绑定

SLF4J:类路径包含多个SLF4J绑定Spring Maven

Apache Storm:SLF4J:类路径包含多个SLF4J绑定

SLF4J:类路径包含多个 SLF4J 绑定警告

slf4j库的冗余包含

类路径包含多个SLF4J绑定

SLF4J:类路径包含多个SLF4J绑定。消息打印在控制台上

Slf4j和类路径错误

在slf4j中设置运行时消息的日志级别

SLF4J:无法加载类“ org.slf4j.impl.StaticLoggerBinder”

RabbitMQ SLF4J:无法加载类“ org.slf4j.impl.StaticLoggerBinder”

SLF4J:无法加载类“ org.slf4j.impl.StaticLoggerBinder”。错误

无法从SLF4J加载类“ org.slf4j.impl.StaticLoggerBinder”消息错误

错误SLF4J的问题:无法加载类“ org.slf4j.impl.StaticLoggerBinder”

SLF4J + Logback未登录WildFly

将Spring示例应用程序部署到Cloud Foundry时出现“ SLF4J:类路径包含多个SLF4J绑定”错误

运行配置单元0.12,错误为slf4j

Velocity中的SimpleLogger(SLF4j)配置错误

如何使用Gradle在IntelliJ中设置SLF4J

如何修复 WebLogic 部署中的 slf4j 错误

Akka SLF4J和Scala中的登录

停止在Java中打印slf4j消息

如何在运行时更改slf4j级别?

覆盖运行时SLF4J绑定以进行测试

在Play 2.3.x启动时如何修复“ SLF4J:类路径包含多个SLF4J绑定”?

与日志库冲突-类路径包含多个SLF4J绑定

类路径包含多个SLF4J绑定Spring Boot排除问题

slf4j,在运行时更改记录器或添加附加程序