Reputation: 1782
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:
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
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:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Upvotes: 2
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
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