当前位置: 代码迷 >> java >> java.lang.NoClassDefFoundError: Lorg/slf4j/Logger
  详细解决方案

java.lang.NoClassDefFoundError: Lorg/slf4j/Logger

热度:101   发布时间:2023-07-18 09:04:11.0

我正在使用具有 maven 支持的 Intellij 启动 tomcat。 添加以下 tomcat maven 依赖项后:

        <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-catalina -->
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-catalina</artifactId>
        <version>8.5.38</version>
    </dependency>

启动时,它会抛出这个异常:

Caused by: java.lang.NoClassDefFoundError: Lorg/slf4j/Logger;
at java.lang.Class.getDeclaredFields0(Native Method)
at java.lang.Class.privateGetDeclaredFields(Class.java:2583)
at java.lang.Class.getDeclaredFields(Class.java:1916)
at org.apache.catalina.util.Introspection.getDeclaredFields(Introspection.java:110)
at org.apache.catalina.startup.WebAnnotationSet.loadFieldsAnnotation(WebAnnotationSet.java:269)
at org.apache.catalina.startup.WebAnnotationSet.loadApplicationListenerAnnotations(WebAnnotationSet.java:89)
at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:67)
at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:328)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:772)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:299)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:94)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5181)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 44 more
Caused by: java.lang.ClassNotFoundException: org.slf4j.Logger
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1364)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1185)
... 57 more

25-Feb-2019 00:51:29.242 SEVERE [RMI TCP Connection(4)-127.0.0.1] org.apache.tomcat.util.modeler.BaseModelMBean.invoke Exception invoking method manageApp
  java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/web_war_exploded]]

我已经检查过我对slf4j依赖是:

        <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.22</version>
    </dependency>

它表明它有类org.slf4j.Logger

如果我删除了 tomcat 依赖项,则一切正常。

更新:

我的pom文件:

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>chrisTina</groupId>
<artifactId>Insbot</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>8</source>
                <target>8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.8.1</version>
    </dependency>

    <!--<dependency>-->
        <!--<groupId>org.apache.logging.log4j</groupId>-->
        <!--<artifactId>log4j-slf4j-impl</artifactId>-->
        <!--<version>2.11.1</version>-->
    <!--</dependency>-->

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.4</version>
        <scope>provided</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.typesafe/config -->
    <dependency>
        <groupId>com.typesafe</groupId>
        <artifactId>config</artifactId>
        <version>1.2.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.8.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.awaitility/awaitility -->
    <dependency>
        <groupId>org.awaitility</groupId>
        <artifactId>awaitility</artifactId>
        <version>3.1.6</version>
        <!--<scope>test</scope>-->
    </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.15</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.4.1.Final</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/net.bytebuddy/byte-buddy -->
    <dependency>
        <groupId>net.bytebuddy</groupId>
        <artifactId>byte-buddy</artifactId>
        <version>1.9.10</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>23.0</version>
    </dependency>

    <!--&lt;!&ndash; https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api &ndash;&gt;-->
    <!--<dependency>-->
        <!--<groupId>javax.servlet</groupId>-->
        <!--<artifactId>javax.servlet-api</artifactId>-->
        <!--<version>4.0.1</version>-->
        <!--<scope>provided</scope>-->
    <!--</dependency>-->


    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.22</version>
    </dependency>

    <!--&lt;!&ndash; https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 &ndash;&gt;-->
    <!--<dependency>-->
        <!--<groupId>org.slf4j</groupId>-->
        <!--<artifactId>slf4j-log4j12</artifactId>-->
        <!--<version>1.7.22</version>-->
    <!--</dependency>-->

    <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-catalina -->
    <!--<dependency>-->
        <!--<groupId>org.apache.tomcat</groupId>-->
        <!--<artifactId>tomcat-catalina</artifactId>-->
        <!--<version>8.5.38</version>-->
    <!--</dependency>-->

</dependencies>

<packaging>war</packaging>

MVN 输出:

    [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ Insbot ---
[INFO] chrisTina:Insbot:war:1.0-SNAPSHOT
[INFO] +- org.seleniumhq.selenium:selenium-java:jar:3.8.1:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-api:jar:3.8.1:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-chrome-driver:jar:3.8.1:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-edge-driver:jar:3.8.1:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-firefox-driver:jar:3.8.1:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-ie-driver:jar:3.8.1:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-opera-driver:jar:3.8.1:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-remote-driver:jar:3.8.1:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-safari-driver:jar:3.8.1:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-support:jar:3.8.1:compile
[INFO] |  +- org.apache.commons:commons-exec:jar:1.3:compile
[INFO] |  +- commons-codec:commons-codec:jar:1.10:compile
[INFO] |  +- commons-logging:commons-logging:jar:1.2:compile
[INFO] |  +- com.google.code.gson:gson:jar:2.8.2:compile
[INFO] |  +- org.apache.httpcomponents:httpclient:jar:4.5.3:compile
[INFO] |  \- org.apache.httpcomponents:httpcore:jar:4.4.6:compile
[INFO] +- org.projectlombok:lombok:jar:1.18.4:provided
[INFO] +- com.typesafe:config:jar:1.2.0:compile
[INFO] +- org.apache.commons:commons-lang3:jar:3.8.1:compile
[INFO] +- org.awaitility:awaitility:jar:3.1.6:compile
[INFO] |  +- org.hamcrest:hamcrest-library:jar:1.3:compile
[INFO] |  +- org.hamcrest:hamcrest-core:jar:1.3:compile
[INFO] |  \- org.objenesis:objenesis:jar:2.6:compile
[INFO] +- mysql:mysql-connector-java:jar:8.0.15:compile
[INFO] |  \- com.google.protobuf:protobuf-java:jar:3.6.1:compile
[INFO] +- org.hibernate:hibernate-core:jar:5.4.1.Final:compile
[INFO] |  +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile
[INFO] |  +- javax.persistence:javax.persistence-api:jar:2.2:compile
[INFO] |  +- org.javassist:javassist:jar:3.24.0-GA:compile
[INFO] |  +- antlr:antlr:jar:2.7.7:compile
[INFO] |  +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.1.1.Final:compile
[INFO] |  +- org.jboss:jandex:jar:2.0.5.Final:compile
[INFO] |  +- com.fasterxml:classmate:jar:1.3.4:compile
[INFO] |  +- javax.activation:javax.activation-api:jar:1.2.0:compile
[INFO] |  +- org.dom4j:dom4j:jar:2.1.1:compile
[INFO] |  +- org.hibernate.common:hibernate-commons-annotations:jar:5.1.0.Final:compile
[INFO] |  +- javax.xml.bind:jaxb-api:jar:2.3.1:compile
[INFO] |  \- org.glassfish.jaxb:jaxb-runtime:jar:2.3.1:compile
[INFO] |     +- org.glassfish.jaxb:txw2:jar:2.3.1:compile
[INFO] |     +- com.sun.istack:istack-commons-runtime:jar:3.0.7:compile
[INFO] |     +- org.jvnet.staxex:stax-ex:jar:1.8:compile
[INFO] |     \- com.sun.xml.fastinfoset:FastInfoset:jar:1.2.15:compile
[INFO] +- net.bytebuddy:byte-buddy:jar:1.9.10:compile
[INFO] +- com.google.guava:guava:jar:23.0:compile
[INFO] |  +- com.google.code.findbugs:jsr305:jar:1.3.9:compile
[INFO] |  +- com.google.errorprone:error_prone_annotations:jar:2.0.18:compile
[INFO] |  +- com.google.j2objc:j2objc-annotations:jar:1.1:compile
[INFO] |  \- org.codehaus.mojo:animal-sniffer-annotations:jar:1.14:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.7.22:compile
[INFO] \- org.apache.tomcat:tomcat-catalina:jar:8.5.38:compile
[INFO]    +- org.apache.tomcat:tomcat-servlet-api:jar:8.5.38:compile
[INFO]    +- org.apache.tomcat:tomcat-jsp-api:jar:8.5.38:compile
[INFO]    |  \- org.apache.tomcat:tomcat-el-api:jar:8.5.38:compile
[INFO]    +- org.apache.tomcat:tomcat-juli:jar:8.5.38:compile
[INFO]    +- org.apache.tomcat:tomcat-annotations-api:jar:8.5.38:compile
[INFO]    +- org.apache.tomcat:tomcat-api:jar:8.5.38:compile
[INFO]    +- org.apache.tomcat:tomcat-jni:jar:8.5.38:compile
[INFO]    +- org.apache.tomcat:tomcat-coyote:jar:8.5.38:compile
[INFO]    +- org.apache.tomcat:tomcat-util:jar:8.5.38:compile
[INFO]    +- org.apache.tomcat:tomcat-util-scan:jar:8.5.38:compile
[INFO]    \- org.apache.tomcat:tomcat-jaspic-api:jar:8.5.38:compile

您需要将 .jar 文件添加到 lib 文件夹$TOMCAT/webapps/$YOUR_WEB_APP/WEB-INF/lib/

在这里添加答案只是因为当我试图调查一个稍微不同的问题时,这个问题是第一个相关的搜索结果:使用 Eclipse 和 Tomcat 运行应用程序 - 会出现相同的错误。 我从来没有意识到 Eclipse(有时?它是否特别适合我的口味?)不会从 Maven 生成的 WAR 文件中复制 lib 目录 - 所以即使必要的 jar 文件在 WAR 中,Eclipse 也没有使用它们 -在 Tomcat 服务器的 wtpwebapps 目录中,在服务器(特别是 Tomcat)上运行/调试时,它使用某种分解结构。 因此,正如其他人在其他需要挖掘的问题中指出的那样,有必要通过项目属性将 Java 构建路径条目 - Maven 依赖项添加到部署程序集 - 其他工具可能会发生类似的事情

PS,是的,正如其他人指出的那样,在 Eclipse 中执行 Maven -> Update Project 经常会弄乱上述配置

PPS 对不起,Eclipse - 以上是因为我的 pom.xml 配置太差 - Eclipse 在每个 Maven 期间所做的 -> 更新项目是将 pom.xml 中的任何配置复制到它自己的项目配置 - 所以当依赖项是没有明确说要在 pom.xml 中复制,它们没有被复制(项目属性被重置) - 我已经按照的答案解决了这个问题

如果您使用 Eclipse,文件.classpath需要指出 lib 路径。 因此,请检查此行是否包含在我们的设置中。 <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/> '

<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
    <attributes>
        <attribute name="maven.pomderived" value="true"/>
        <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
    </attributes>
</classpathentry>
  相关解决方案