Compilation failure: module not found: org.apache.logging.log4j
Asked Answered
T

2

8

I have simple application written in Java 11. mvn clean verify (maven 3.6.0) executes with error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project parser: Compilation failure
[ERROR] ...src/main/java/module-info.java:[2,32] module not found: org.apache.logging.log4j  

Dependencies:

<log4j.version>2.11.1</log4j.version>

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>${log4j.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>${log4j.version}</version>
</dependency>

Module-info.java:

module abc {
    requires org.apache.logging.log4j;
}

Log4j2 configuration is default and in .xml file. Usage:

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

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

logger.info("Boom!");

I tried all related questions on stackoverflow with no success.

Turgeon answered 15/11, 2018 at 19:31 Comment(3)
Could you try and upgrade your maven-compiler-plugin and specify the release as 11? Since describing the log4j-api jar using --release 11 states the same name as in your directive. The reason primarily being that log4j-api is a modular jar. Do note, on the other hand log4j-core is derived as an automatic module.Callboy
Thank you! It helped. Do you want to write an answer? Better to use referenced answer from your link. in my case I just needed to upgrade version of maven compiler plugin to 3.8.0. I had java version as 11.Turgeon
Made an answer since there seems to be a slight impact of using the release flag specifically for log4j-api, that being a modular jar. Also, maybe some contributor from the library themselves pitch in with further details over modularisation and plans ahead in consuming the library.Callboy
C
3

You shall upgrade to the maven-compiler-plugin:3.8.0 and specify the release as 11.

Reason being, on describing the log4j-api.jar using --release 11 states the same name as in your directive.

jar --file=.../.m2/repository/org/apache/logging/log4j/log4j-api/2.11.1/log4j-api-2.11.1.jar --describe-module --release 11
releases: 9

org.apache.logging.log4j jar:file://.../.m2/repository/org/apache/logging/log4j/log4j-api/2.11.1/log4j-api-2.11.1.jar/!META-INF/versions/9/module-info.class
exports org.apache.logging.log4j
exports org.apache.logging.log4j.message
exports org.apache.logging.log4j.simple
exports org.apache.logging.log4j.spi
exports org.apache.logging.log4j.status
exports org.apache.logging.log4j.util
requires java.base mandated
uses org.apache.logging.log4j.message.ThreadDumpMessage$ThreadInfoFactory
uses org.apache.logging.log4j.spi.Provider
uses org.apache.logging.log4j.util.PropertySource

which is primarily because the log4j-api is a modular jar.

On the other hand, log4j-core is derived as an automatic module still which is overridden in its MANIFEST.MF as

org.apache.logging.log4j.core
Callboy answered 15/11, 2018 at 20:12 Comment(0)
S
0

Despite changing updating the compiler plugin as mentioned by @Naman similar error might occur if you are using maven javadoc plugin to generate javadocs with command like 'mvn clean install javadoc:javadoc':

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-javadoc-plugin:3.2.0:javadoc (default-cli) on project data-exchange-common-configuration: An error has occurred in Javadoc report generation:       
[ERROR] Exit code: 1 - your.module\src\main\java\module-info.java:20: error: module not found: org.apache.logging.log4j          
[ERROR]   requires org.apache.logging.log4j;                                                                                                                                                                      
[ERROR]                              ^                                                                                                                                                                            
[ERROR]                                                                                                                                                                                                           
[ERROR] Command line was: cmd.exe /X /C "C:\Programs\Java\Oracle\jdk-11.0.6\bin\javadoc.exe @options @packages @argfile"                                                                                          
[ERROR]                                                                                                                                                                                                           
[ERROR] Refer to the generated Javadoc files in 'C:\Source\dex\dex1\ais-data-exchange\data-exchange-common\data-exchange-common-configuration\target\site\apidocs' dir.                                           

The solution for now (until the problem is resolved on the plugin level) is to generate the api docs setting source version as java 8 (while keeping compiler version still as java 9+):

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <source>8</source>
                    <detectJavaApiLink>false</detectJavaApiLink>
                </configuration>
            </plugin>
            

In above example please see that I also set false that is due to some other bug which is there while generting sources in java 8. Anyway setting source param to 8 and detectJavaApiLink solves the issue.

Sevenfold answered 15/4, 2021 at 17:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.