javamavenjava-platform-module-systemdnsjava

DNSJava 2.1.8 as dependency invalidates JPMS (Java 9 Platform Module System) compatibility


There is a maven artifact called xxx:

Having this vanilla pom:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>aaa</groupId>
    <artifactId>xxx</artifactId>
    <properties>
        <java.version>17</java.version>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <maven.compiler.source>${java.version}</maven.compiler.source>
    </properties>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>dnsjava</groupId>
            <artifactId>dnsjava</artifactId>
            <version>2.1.8</version>
        </dependency>
    </dependencies>
</project>

And this java-class (Test):

package xxx;

import org.xbill.DNS.Lookup;

public class Test {
    public static void main(final String[] args) {
        Lookup.class.getSimpleName();
    }
}

Now, the artifact should be a module (Java 9 Platform Module System). So I ask eclipse to create that module-info.java

enter image description here

This works fine so far. The module-info.java have this contents:

module xxx {
    exports xxx;
    requires dnsjava;
}

Then I try to build the jar using mvn install. But the Build faild. This is the output:

grim@main:~/workspace/xxx$ mvn install
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------< aaa:xxx >-------------------------------
[INFO] Building xxx 0.0.1-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- resources:3.3.0:resources (default-resources) @ xxx ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/grim/workspace/xxx/src/main/resources
[INFO] 
[INFO] --- compiler:3.10.1:compile (default-compile) @ xxx ---
[WARNING] Can't extract module name from dnsjava-2.1.8.jar: lookup.class found in top-level directory (unnamed package not allowed in module)
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to /home/grim/workspace/xxx/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /home/grim/workspace/xxx/src/main/java/module-info.java:[4,18] module not found: dnsjava
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.278 s
[INFO] Finished at: 2023-10-13T22:36:07+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project xxx: Compilation failure
[ERROR] /home/grim/workspace/xxx/src/main/java/module-info.java:[4,18] module not found: dnsjava
[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
grim@main:~/workspace/xxx$ 

Any ideas?


Solution

  • The problem is that dnsjava-2.1.8.jar contains classes in the root of the JAR. This is not allowed for a modular JAR, so Java will not consider it a module. A module cannot have any classes in the unnamed package (other than module-info.class). As a result no module called dnsjava is present, leading to the error "module not found: dnsjava".

    This is also reported in your Maven output:

    [WARNING] Can't extract module name from dnsjava-2.1.8.jar: lookup.class found in top-level directory (unnamed package not allowed in module)

    However, the solution to your problem might be easy: upgrade to a newer version of dnsjava. For example, version 3.5.2 — the latest version at this time — declares an Automatic-Module-Name in its manifest, org.dnsjava (this seems to have been introduced in version 3.0.0), and does not contain any classes in its root.

    In short:

    1. Upgrade to version 3.5.2
    2. Use requires org.dnsjava; instead of requires dnsjava; in module-info.java