Log4j2 java.lang.NoClassDefFoundError: org / apache / logging / log4j / LogManager

I am using log4j 2.3 in my java application. I added the dependency via maven.
When running the program in eclipse, everything works fine, but when I package it using maven and try to run jar, I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache logging/log4j/LogManager at main.myclass.<clinit>(myclass.java:11) Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.LogManager at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 1 more 

Why can't he find a class during his launch from the can?

Adding log4j 1.2 did not work either. The program works fine in an eclipse, so there should be no missing dependency.

+8
source share
2 answers

When you start your application jar from the command line, your dependent jar is not available at run time. You need to include either of these two plugins in pom.xml so that your dependencies are available at runtime.

Usage: maven-shade-plugin

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>org.sonatype.haven.HavenCli</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> 

Usage: Maven-dependency-plugin

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.8</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> 

When you run the mvn package , it will generate a uber jar / or copy the dependencies into outputDirectory. I would prefer maven-shade-plugin to generate one jar, all dependencies.

+12
source

Install the latest version of log4j (I installed log4j-2.3.jar ).

And follow these steps:

  • Right-click the project -> Build Path -> Libraries -> Add External Jar Files -> Include Log4j, Log4j core and Log4j api files.

It worked for me

0
source

Source: https://habr.com/ru/post/976668/


All Articles