Maven project: module not found even with correct dependencies

135 Views Asked by At

I'm trying to build a Java module with Maven but I keep getting the same error (module not found) with every dependency when I run mvn compile. Neither Intellij IDEA nor VSCode give me any error before compiling.

I think it's a JPMS proble since if I delete module-info.java it works flawlessly.

java version:openjdk 20.0.2 2023-07-18

IntelliJ IDEA 2023.1.4 (Community Edition)

Apache Maven 3.8.7

I built a smaller example which represents the problem, note that I get the same error with other dependencies like jakarta.xml.bind or Docx4J.

myapp
|---pom.xml
|---src/main/java
      |---module-info.java
      |---com/andreagenovese/myapp
           |---Main.java

Main.java

package com.andreagenovese.myapp;

import org.apache.logging.log4j.LogManager;

public class Main {
    public static void main(String[] args) {
        LogManager.getLogger();
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.andreagenovese</groupId>
    <artifactId>myapp</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>20</maven.compiler.source>
        <maven.compiler.target>20</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.13.0</version>
        </dependency>
        
         <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.13.0</version>
        </dependency>
    </dependencies>
</project>

module-info.java

module com.andreagenovese.myapp {
    requires org.apache.logging.log4j;
    exports com.andreagenovese.myapp;
}

The error

[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ myapp ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /home/geno/Documents/Programmi/Java/myapp/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /home/geno/Documents/Programmi/Java/myapp/src/main/java/module-info.java:[2,32] module not found: org.apache.logging.log4j
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.816 s
[INFO] Finished at: 2023-08-04T00:32:03+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project myapp: Compilation failure
[ERROR] /home/geno/Documents/Programmi/Java/myapp/src/main/java/module-info.java:[2,32] module not found: org.apache.logging.log4j
[ERROR] 
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

1

There are 1 best solutions below

1
Long On

Log4j will not work with only core library. You can try with these dependency :

        <properties>
             <log4j.version>2.13.0</log4j.version>
        <properties>

        <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-to-slf4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j.version}</version>
        </dependency>