ClassNotFoundException: org.apache.logging.log4j.Logger

538 Views Asked by At

In my Java 17 maven project I am using slf4j based on log4j2. It runs fine, when I start it from within Eclipse.
Maven places the main class in MANIFEST of the modules jar file and all dependent jar files in the target/lib folder.
target/lib hosts the file log4j-api-3.0.0-alpha1.jar which hosts the class org.apache.logging.log4j.Logger.

I have check the precense of org.apache.logging.log4j.Logger with the bash command

for i in target/lib/*.jar; do jar -tvf "$i" | grep -Hsi org.apache.logging.log4j.Logger; done

When I run my program from the command line with

java -p target/lib -m eu.ngong.iqStm

I get the failure

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/logging/log4j/Logger
        at [email protected]/org.apache.logging.slf4j.SLF4JServiceProvider.initialize(SLF4JServiceProvider.java:53)
        at [email protected]/org.slf4j.LoggerFactory.bind(LoggerFactory.java:195)
        at [email protected]/org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:182)
        at [email protected]/org.slf4j.LoggerFactory.getProvider(LoggerFactory.java:490)
        at [email protected]/org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:476)
        at [email protected]/org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:425)
        at [email protected]/org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:451)
        at [email protected]/eu.ngong.iqStm.App.<clinit>(App.java:30)
Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.Logger
        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)
        ... 8 more
        

An idea to narrow the reason why java can not find the class org.apache.logging.log4j.Logger?

Any idea what I have to add on the command line to run my program?

1

There are 1 best solutions below

0
Piotr P. Karwasz On

Since log4j-slf4j2-impl in the alpha1 version is not a full module, the JVM is not able to find its dependencies. You need to add the module for log4j-api explicitly:

java -p target/lib --add-modules org.apache.logging.log4j -m eu.ngong.iqStm

In the next alpha version all modules will be named JPMS modules, so this problem will not appear.