简体   繁体   中英

java.lang.NoClassDefFoundError: Lorg/slf4j/Logger

I am launching tomcat using Intellij with maven support. Once I add the following tomcat maven 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>

Upon launching, it will throw this exception:

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]]

I have checked that my dependency of slf4j is:

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

And it shows that it has class org.slf4j.Logger :

If I remove the tomcat dependency, everything works fine again.

在此处输入图片说明

Update:

My pom file:

<?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 output:

    [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/

adding an answer here just because this question was the first relevant search result when i was trying to investigate a slightly different issue : running an application with Eclipse and Tomcat - same error would appear. I never realized that Eclipse (sometimes? was it particular to my flavour?) wouldn't copy the lib directory from within the Maven-generated WAR file - so even though the necessary jar files were inside WAR, Eclipse wasn't using them - it uses some kind of exploded structure when it comes to running/debugging on server (Tomcat in particular), within wtpwebapps directory of the Tomcat server. So as others have pointed out in other questions that needed to be dug out, it is necessary to add Java Build Path Entries - Maven Dependencies to Deployment Assembly via Project Properties - it is possible that a similar thing would happen with other tools

PS and yes, as others have pointed out, doing Maven -> Update Project in Eclipse often messes up the aforementioned configuration

PPS I am sorry, Eclipse - the above was because of my seedy pom.xml configuration - what Eclipse does during each Maven -> Update Project is to copy whatever configuration is in pom.xml to its own project configuration - so when the dependencies are not explicitly said to be copied in the pom.xml, they are not copied (Project properties are reset) - I have fixed this issue following the answer from here

If you are using Eclipse, the file .classpath needs to point out the lib path. So check that this line are included in our settings. <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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM