0

I was trying out Log4j2 so I created a Java project in eclipse. My code is same as Log4j2 Manual Automatic Configuration part. I have added log4j-api-2.17.2.jar and log4j-core-2.17.2.jar in the classpath. When I run the application I get following error

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/logging/log4j/LogManager
    at demo.one.MainClass.<clinit>(MainClass.java:8)
Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.LogManager
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)

I have checked some related questions but didn't find those useful.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
Mahmud Mridul
  • 95
  • 1
  • 9

3 Answers3

0

In case you use maven, make sure to include Log4j core to the project:

<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.18.0</version>
</dependency>
Queeg
  • 7,748
  • 1
  • 16
  • 42
0

Since, it's a NoClassDefFoundError, that means your JRE is not able to find out the necessary jar(class) artifact holding definition for org.apache.logging.log4j.LogManager. Look for differences between build and runtime classpaths.

Jaraws
  • 581
  • 1
  • 7
  • 24
0

how did u run the code? If from terminal, you might want to add maven-shade-plugin https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html

keepscoding
  • 146
  • 1
  • 7
  • Please don't just link to a tool or library. Instead, also include details about what it does, how it addresses the OP's question, and ideally how to use it to solve the specifics of the case. **Would you kindly [edit] your answer to include this information?** – Jeremy Caney May 26 '23 at 02:39
  • Check also [this answer](https://stackoverflow.com/a/76015329/11748454) for the 3 resource transformers required by Log4j 2.x. – Piotr P. Karwasz May 26 '23 at 06:52