Daredevil
Daredevil

Reputation: 1782

java- NoClassDefFoundERROR: LogManager

I am trying to program a simple log4j2 logging message.

This is how the code looks like:

package com.company;

import java.io.IOException;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;


public class Main {

   private static final Logger logger = LogManager.getLogger(Main.class);

    public static void main(String[] args) throws IOException {

        String message = "Hello there!";
        System.out.println(message);
        logger.info(message);

    }
}

And my external libraries importing the API:

Screenshot

When I run the program, it throws an :

 java.lang.NoClassDefFoundError: org/apache/logging/log4j/LogManager
 Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.LogManager

I have already imported the relevant jar files but do not understand why do I still get this error.

Anything I am doing wrong?

Upvotes: 3

Views: 11390

Answers (3)

Balazs F.
Balazs F.

Reputation: 410

You want to use log4j2, but instead you are using log4j as import. To make your project work you have to do 2 things:

  1. As @Huan Zhang suggested, add imports
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
  1. Remove all jars starting with log4j-1.x. (like first jar in your screenshot). I made the same mistake, because when I downloaded the log4j dependency package, I didnt know which files I need, so added all of them. But by doing so for whatever reason now log4j 1 got activated instead of log4j 2.

Upvotes: 2

Michael Butscher
Michael Butscher

Reputation: 10959

Instead of the actual library log4j-api-2.11.1.jar you included only the API bridge log4j-1.2-api-2.11.1.jar in the classpath. It translates your API 1 calls to API 2 calls. But the actual library handling the calls is missing.

Add the actual library to classpath and either remove the bridge and rewrite the API calls (recommended) or keep the bridge and stay with API 1 calls.

Upvotes: 3

Huan Zhang
Huan Zhang

Reputation: 42

Try import LogManager and Logger with the following code:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Upvotes: -2

Related Questions