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.
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
file.ext.moduleName = 'com.example'
tasks.compileJava {
inputs.property('moduleName', moduleName)
doFirst {
options.compilerArgs = [
'--module-path', classpath.asPath
]
classpath = files()
}
}
My test project
HelloMath
├── build.gradle
└── src
└── main
└── java
├── com
│ └── example
│ └── HelloMathApp.java
└── module-info.java
plugins {
id 'java'
id 'application'
}
application {
mainClass = 'com.example.HelloMathApp'
//module
//mainClass = 'com.example/com.example.HelloMathApp'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
description = 'Hello Apache Commons Math App Project'
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
modularity.inferModulePath = true
}
ext.moduleName = 'com.example'
tasks.compileJava {
inputs.property('moduleName', moduleName)
doFirst {
options.compilerArgs = [
'--module-path', classpath.asPath
]
classpath = files()
}
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
repositories {
mavenCentral()
}
dependencies {
// Apache Commons Math 4 Legacy
//implementation 'org.apache.commons:commons-math4-legacy:4.0-beta1'
// Apache Commons Math 3
implementation 'org.apache.commons:commons-math3:3.6.1'
}
tasks.jar {
archiveBaseName = 'app'
}
tasks.register('copyDependencies', Copy) {
def outputDir = layout.buildDirectory.dir('libs')
from configurations.runtimeClasspath
into outputDir
}
module com.example {
requires commons.math3;
requires java.base;
exports com.example;
}
package com.example;
//org.apache.commons:commons-math3:3.6.1
import org.apache.commons.math3.analysis.function.Abs;
//org.apache.commons:commons-math4-legacy:4.0-beta1
//import org.apache.commons.math4.legacy.analysis.function.Abs;
public class HelloMathApp {
public static void main(String... args) {
Abs abs = new Abs();
System.out.println(abs.value(-10.0d));
}
}
Open Terminal:
gradle clean run
Output
> Task :run
10.0
gradle clean build
HelloMath
├── build
│ ├── distributions <-- plugins 'application'
│ │ ├── HelloMath-2-0.0.1-SNAPSHOT.tar
│ │ └── HelloMath-2-0.0.1-SNAPSHOT.zip
│ ├── libs
│ │ └── app-0.0.1-SNAPSHOT.jar
gradle copyDependencies
HelloMath
├── build
│ └── libs
│ ├── app-0.0.1-SNAPSHOT.jar
│ └── commons-math3-3.6.1.jar <-- Dependencies
java \
-cp "build/libs/*" \
com.example.HelloMathApp
Output
10.0
2-0.0.1
, you must change to your numberbuild/distributions/HelloMath-2-0.0.1-SNAPSHOT.tar
HelloMath/build/distributions/HelloMath-2-0.0.1-SNAPSHOT
├── bin
│ ├── HelloMath
│ └── HelloMath.bat
└── lib
├── app-0.0.1-SNAPSHOT.jar
└── commons-math3-3.6.1.jar
cd build/distributions/HelloMath-2-0.0.1-SNAPSHOT/bin
./HelloMath
Output
10.0
HelloMath is generated by plugins application.
cd build/distributions/HelloMath-2-0.0.1-SNAPSHOT
java --module-path lib --module com.example/com.example.HelloMathApp
Output
10.0