首页 > 解决方案 > 程序运行时未在类路径中包含 slf4j

问题描述

我一直没有成功地尝试包含slf4j在一个项目中。我已经将slf4j-api-1.7.32.jarand添加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. 现在需要找出部署脚本中需要更改的内容。

标签: javaeclipseclasspathslf4j

解决方案


解决方案:

将以下“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 文件来加载它们。现在systemClassLoaderhas 父类型URLClassLoader有一个字段ucp( ),其中包含所有可以从中加载类的 .jar 文件URLClassPath的列表(在 下)。path看看这里,slf4j jar 确实存在,但由于某种原因isServerPath返回了 true。这最终是导致找到上述解决方案的原因。


推荐阅读