Home > Back-end >  "Module not found, required by" when implementing maven library via gradle using javafx?
"Module not found, required by" when implementing maven library via gradle using javafx?

Time:02-08

I am using gradle 7.3.1 in Intellij 2021.3 to build a JavaFX application. And I need external libraries dependency for the backend part.

This is the complete build.gradle script that is pre-built by the idea:

plugins {
    id 'java'
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.10'
    id 'org.beryx.jlink' version '2.24.1'
}

group 'com.github.zukarusan'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

ext {
    junitVersion = '5.8.1'
}

sourceCompatibility = '11'
targetCompatibility = '11'

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

application {
    mainModule = 'com.github.zukarusan.app'
    mainClass = 'com.github.zukarusan.app.MainApplication'
}

javafx {
    version = '11.0.2'
    modules = ['javafx.controls', 'javafx.fxml']
}

dependencies {
    implementation('org.controlsfx:controlsfx:11.1.0')
    implementation('com.dlsc.formsfx:formsfx-core:11.3.2') {
        exclude(group: 'org.openjfx')
    }
    implementation('org.kordamp.ikonli:ikonli-javafx:12.2.0')
    implementation('org.kordamp.bootstrapfx:bootstrapfx-core:0.4.0')

    implementation('com.github.wendykierp:JTransforms:3.1') // THIS IS THE EXTERNAL LIBRARY I ADDED

    testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
}

test {
    useJUnitPlatform()
}

jlink {
    imageZip = project.file("${buildDir}/distributions/app-${javafx.platform.classifier}.zip")
    options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
    launcher {
        name = 'app'
    }
}

jlinkZip {
    group = 'distribution'
}

and then I updated module-info.java:

module com.github.zukarusan.app {
    requires javafx.controls;
    requires javafx.fxml;
    requires org.controlsfx.controls;
    requires com.dlsc.formsfx;
    requires org.kordamp.ikonli.javafx;
    requires org.kordamp.bootstrapfx.core;
    requires java.desktop;

    requires JTransforms; // UPDATED

    exports com.github.zukarusan.app.controller;
    exports com.github.zukarusan.app.model;
    exports com.github.zukarusan.app;
    opens com.github.zukarusan.app.controller to javafx.fxml;
}

Before I updated the module-info , the application launches just fine and shows a javafx window application. But, after I updated that, java.lang.FindException is thrown. resulting the following error:

> Task :MainApplication.main() FAILED
Error occurred during initialization of boot layer
java.lang.module.FindException: Module JTransforms not found, required by com.github.zukarusan.app

Do I have to download manually the modules? or any steps that I miss?


Please enlighten me. :'(

CodePudding user response:

Guided by @jewelsea in the comment, I have finally resolved this issue.

After several days, I figured out that my imported libraries in modular java project are treated as an unnamed module (correct me if I am wrong). As I understood so far, libraries like JTransforms may belong to traditional libraries that do not cover the modularity feature like in Java 9. This means that the jar library does not have Automatic-Module-Name at the very least in its manifest metadata. See the gradle documentation.

That is to say, I will have another approach using a grade plugin extra-java-module-info and found a somewhat similar case in this post.

Ultimately, I add the following script in build.gradle

plugins {
    ... // other plugins

    id 'de.jjohannes.extra-java-module-info' version '0.11'
}

extraJavaModuleInfo {
    module("JTransforms-3.1.jar", "JTransforms", "3.1") {
        exports("org.jtransforms")
    }
}

Then hit that sync button. That's all!

Cheers! Thanks again to @jewelsea.

  •  Tags:  
  • Related