10

I am trying to setup a project with slf4j + log4j, but the damn thing just doesn't work... I keep getting exception:

Failed to instantiate SLF4J LoggerFactory
Reported exception:
java.lang.NoClassDefFoundError: org/apache/log4j/Level
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:129)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:108)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:302)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:276)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:288)
at TestLog.main(TestLog.java:9)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Level
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 6 more
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Level
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:129)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:108)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:302)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:276)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:288)
at TestLog.main(TestLog.java:9)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Level
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 6 more

I checked and the class org.apache.log4j.Level is there in my project under "Maven Dependencies > log4j-1.2.17.jar". So why can't the VM find it?

I tried the below options already:

  1. slf4j-api + log4j
  2. slf4j-api + slf4j-log4j12
  3. slf4j-api + log4j
  4. slf4j-api + slf4j-log4j12 + log4j

None of the options worked :(

Here is my currnet pom.xml:

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.sample</groupId>
  <artifactId>DBUnitSample</artifactId>
  <version>1.0</version>

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

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.7</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>

  </dependencies>

</project>

Also, I noticed that when adding slf4j-log4j12, it adds log4j as a dependency as well in the tree, so I am guessing that adding log4j explicitly here is redundant, but I have added it anyway. Still no luck.

My main code is pretty simple:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SampleDao {
    private static final Logger log = LoggerFactory.getLogger(SampleDao.class);

    public static void main(String[] args) {
        System.out.println("Hello");
        log.info("Logged");
    }
}

And my log4j.properties:

log4j.rootLogger=DEBUG, STDOUT
log4j.logger.deng=INFO
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

Can someone please tell me what am I doing wrong? I already tried searching for answers on SO, but all answers mentioned to add log4j dependency, which I have already added... So not sure, whats wrong.

EDIT: Here is my entire project structure. I am R-clicking SampleDao and running it as Java application. ( I have also removed dbunit dependency for now, to zero-in on the slf4j problem) Maven Project Structure

Pat
  • 2,223
  • 4
  • 19
  • 25
  • Works for me, maybe your problem is that you don't specify the jars in the classpath when you run your program – morgano Sep 06 '14 at 21:03
  • How do you run you program? Try to add some explicit invoking for log4j, if that works, the reason will be your construction tools. – Loki Sep 07 '14 at 03:57
  • I am using embedded maven in Eclipse and I have created this as a maven project. So I am using Eclipse Run Configurations to run the class as Java application (R-click > Run as > Java Application). I also checked the classpath in Run Configurations, and I can see that Maven Dependencies are part of it. So not sure why log4j isnt discovered. – Pat Sep 07 '14 at 08:04
  • 1
    Try getting this running in plain command line Maven first. Then see if you can get it running in Eclipse. Eclipse and Maven don't always play together nicely. – user944849 Sep 08 '14 at 14:03
  • ok I got rid of maven altogether an created a normal java project, added the dependencies manually as external jars and still no luck. I also tried running eclipse with Admin account, but still didnt work. Though, the same code worked fine on another machine with same JDK, same architecture. I guess, there is some environment issue. Trying to zero down on it now. Thanks for help though. – Pat Sep 08 '14 at 16:13
  • 1
    ok got it working finally. It seems, my log4j was corrupted it seems. Got an error while running from cmd line `log4j-1.2.17.jar; invalid LOC header (bad signature)` downloaded again and it worked! :facepalm: – Pat Sep 08 '14 at 16:55

5 Answers5

4

Try these dependencies

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.5</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>1.7.5</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.5</version>
    <scope>runtime</scope>
</dependency>
Dmytro Plekhotkin
  • 1,965
  • 2
  • 23
  • 47
  • 1
    tried it, same error. Moreover, I think, with `slf4j-log4j12` added, we dont need anything else. Also, as per SLF4J doc, it is advised against adding multiple bindings: `Do not place more than one binding on your class path` (http://www.slf4j.org/manual.html) So I am not sure, if adding JCL and Log4J both is a good idea. – Pat Sep 07 '14 at 14:56
  • but I am not using activemq. I dont get it, how is it related to slf4j issue that I have? – Pat Sep 07 '14 at 17:26
1

Do you still get the error if you run mvn compile exec:java -D"exec.mainClass"=SampleDao from the command line? I see the following on the command line after running this command, with Maven 3.1.1 on my machine and your code:

Hello
 INFO [SampleDao.main()] (SampleDao.java:9) - Logged

So, your POM and code seem to be fine. It looks like something is wrong with how you are trying to run the code.

Also note that the following simplified configuration should work:

<dependencies>
  <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.7</version>
  </dependency>
</dependencies>

You would have to specify a particular version of Log4j if the POM has other dependencies that add different versions of Log4j to the project.

manish
  • 19,695
  • 5
  • 67
  • 91
1

I met the same mistake as you. I solved my problem according to the following two Suggestions.I hope it helps.

1. Modify your pom file:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.2</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.12</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.2</version>
</dependency>

2. Add the jar to your startup script

java -cp ./lib/log-producer-1.0.4-SNAPSHOT.jar:./lib/slf4j-log4j12-1.7.2.jar:./lib/slf4j-api-1.7.2.jar:./lib/log4j-1.2.12.jar cn.tiakon.core.Bootstrap
Paul Lemarchand
  • 2,068
  • 1
  • 15
  • 27
kai.tian
  • 31
  • 1
  • 5
  • https://stackoverflow.com/questions/15246157/how-to-fix-exception-failed-to-instantiate-slf4j-loggerfactory – kai.tian May 16 '19 at 18:11
0

Use this depedences.

<dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.30</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.30</version>
    </dependency>

Config file:

src/main/resources/log4j.properties
Ronald Coarite
  • 4,460
  • 27
  • 31
-1

Add below dependency :

<dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>${logback.version}</version>
        </dependency>