2

I'm trying a Spring 3.x tutorial on Eclipse 3.7 with m2e and Maven Integration for WTP plugins installed.

I converted the above tutorial to Maven Project, but when i deploy the web application i get the following exception:

Failed to instantiate SLF4J LoggerFactory
Reported exception:
  java.lang.NoClassDefFoundError: org/apache/log4j/Level

My pom.xml look like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.springframework.samples</groupId>
    <artifactId>mvc-basic</artifactId>
    <name>mvc-basic</name>
    <packaging>war</packaging>
    <version>1.0.0-SNAPSHOT</version>
    <properties>
        <org.springframework.version>3.0.5.RELEASE</org.springframework.version>
        <org.slf4j.version>1.6.1</org.slf4j.version>
    </properties>
    <dependencies>

        <!--  Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework.version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                 </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${org.slf4j.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
            <scope>runtime</scope>
        </dependency>

        <!--  JSR 303 with Hibernate Validator -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.1.0.Final</version>
        </dependency>

        <!--  Joda Time -->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>1.6.2</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time-jsptags</artifactId>
            <version>1.0.2</version>
            <scope>runtime</scope>
        </dependency>       

        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <repositories>
        <!-- For Hibernate Validator -->
        <repository>
            <id>org.jboss.repository.release</id>
            <name>JBoss Maven Release Repository</name>         
            <url>https://repository.jboss.org/nexus/content/repositories/releases</url>
            <snapshots><enabled>false</enabled></snapshots>         
        </repository>
    </repositories>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <warName>mvc-basic</warName>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>install</id>
                        <phase>install</phase>
                        <goals>
                            <goal>sources</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

I also checked in 'Project Properties' -> 'Java Build Path' -> 'Libraries' but the only log4j jar that i found is the one under 'Maven Dependencies'

Seems like i'm messing with jars but i can't find a solution by myself.

Piwakkio
  • 63
  • 1
  • 3
  • 12
  • there is another pom entry for slf org.slf4j slf4j-api ${org.slf4j.version} runtime – Chetan Feb 15 '12 at 21:53
  • possible duplicate of [How to fix exception: Failed to instantiate SLF4J LoggerFactory?](http://stackoverflow.com/questions/15246157/how-to-fix-exception-failed-to-instantiate-slf4j-loggerfactory) – kenorb Apr 09 '15 at 21:29

2 Answers2

0

Try to clean the project and server in eclipse. It looks like a refreshing problem.

If you use Maven (M2E) in Eclipse then it is correct that the libs are only in the "Maven Dependencies Section".

May you start with an Spring Source Tool Suite Template Project (that is maven based) and then you copy the code from the tutorial into this project.

If you use Tomcat, then Eclipse must copy the libs to workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp<VERSION>\wtpwebapps\<PROJECT>\WEB-INF\lib. If the lib is not there, and eclipse does not copy it even if you refersh every thing. Then sometimes the last thing you can do is, Remove the project from the server (in the "Add and Remove..." Dialog) press Finish, and then add the project again.

Ralph
  • 118,862
  • 56
  • 287
  • 383
  • Tryed refreshing project (F5), updating project configuration with Maven, clean Tomcat Work Directory and republish project from scratch, but problem is still here. – Piwakkio Feb 15 '12 at 21:38
  • only two warnings, saying the jdk is not 'strictly compatible' (1.7 instead of 1.5) and a static final UID is missing in a serializable class – Piwakkio Feb 15 '12 at 21:43
  • Warnings should be no problem - try my tip with remove and add - if this does not help, then it is not a refreshing problem. - To verify this try to build the war with maven from command line and deploy the war by hand. – Ralph Feb 15 '12 at 21:44
  • I tryed making a STS Template Project from scratch and running it on Tomcat, but same error occourred. Seems like it is not a refreshing problem. – Piwakkio Feb 16 '12 at 18:30
0

Looks like some other library has dependency on SLF4J. Create a WAR file and check if SLF4J.jar is packaged in the war. If not, check your Appservers common lib directory. In case its loading it. Check the Dependency graph of your pom.xml.

In case if you want to add SLF4J log4j API below is the dependency.

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.6.4</version>
</dependency>
Rajendra
  • 1,703
  • 2
  • 16
  • 22
  • I made a WAR of the project, and found that i miss SLF4J.jar but i do have the SLF4J-log4j12-1.6.1.jar that seems to be the one you posted the dependency. Is that correct? – Piwakkio Feb 16 '12 at 18:39