Search code examples
javagradleapache-commons-math

Error: module not found for Apache Commons Math (commons-math3) when using Gradle and Java modules


I'm working on a Java 17 project using Gradle with the Java module system, and I'm encountering an issue when trying to include the Apache Commons Math (commons-math3) library in my project.

What I am trying to do: I have a 'module-info.java' file where I'm trying to add the following dependencies:

java
module com.example {
    requires org.apache.pdfbox;
    requires org.apache.poi.ooxml;
    requires commons.math3;
    requires org.apache.commons.collections4;
    exports org.example;
    }

In my build.gradle, I have the following dependencies declared:

dependencies {
        implementation 'org.apache.pdfbox:pdfbox:2.0.30'
        implementation 'org.apache.poi:poi-ooxml:5.3.0'
        implementation 'org.apache.commons:commons-math3:3.6'
    }

When I try to build the project using gradle build or run it via IntelliJ, I get the following error:

error: module not found: commons.math3
        requires commons.math3;

Checking if the dependency is downloaded correctly with gradlew dependencies.


Solution

  • module-info.java

    • Change exports org.example; to exports com.example;.
    module com.example {
        requires org.apache.pdfbox;
        requires org.apache.poi.ooxml;
        requires commons.math3;
        requires org.apache.commons.collections4;
        exports com.example;
        }
    

    build.gradle

    • Add the following content to the build.gradle file.
    ext.moduleName = 'com.example'
    
    tasks.compileJava {
        inputs.property('moduleName', moduleName)
        doFirst {
            options.compilerArgs = [
                    '--module-path', classpath.asPath
            ]
            classpath = files()
        }
    }