So beheben Sie den Modul liest Paketfehler in Java9

Lesezeit: 7 Minuten

Benutzer-Avatar
hudi

Ich versuche, die neue Modularität in Java 9 mit Spring-Boot zu verstehen, also möchte ich zum Beispiel eine einfache Anwendung ausführen: https://github.com/tmatyashovsky/java9-springboot

Ich verwende Maven 3.5.0 mit Java 9:

Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-03T21:39:06+02:00)
Maven home: ~/soft/apache-maven-3.5.0
Java version: 9-ea, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-9-oracle
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.4.0-79-generic", arch: "amd64", family: "unix"

Das Problem ist, dass ich immer noch eine Ausnahme habe. Was bedeutet das und wie soll ich es beheben?

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.1:compile (default-compile) on project api: Compilation failure: Compilation failure: 
[ERROR] module  reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module spring.core reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module spring.jcl reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module spring.aop reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module spring.expression reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module spring.boot.starter.web reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module spring.boot.starter reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module spring.boot reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module spring.boot.autoconfigure reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module spring.boot.starter.logging reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module logback.classic reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module logback.core reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module slf4j.api reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module jul.to.slf4j reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module log4j.over.slf4j reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module spring.boot.starter.json reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module jackson.databind reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module jackson.annotations reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module jackson.core reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module jackson.datatype.jdk8 reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module jackson.datatype.jsr310 reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module jackson.module.parameter.names reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module spring.boot.starter.tomcat reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module tomcat.embed.core reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module tomcat.embed.el reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module tomcat.embed.websocket reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module hibernate.validator reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module validation.api reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module jboss.logging reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module classmate reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module spring.web reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module spring.webmvc reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module spring.context reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module spring.beans reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module com.lohika.morning.java9modules.service reads package javax.annotation from both java.xml.ws.annotation and tomcat.embed.core

  • Welche genaue Version von JDK9 verwenden Sie?

    – khmarbaise

    22. Juni 2017 um 13:03 Uhr

  • neuesten frühen Zugriff

    – hudi

    22. Juni 2017 um 13:04 Uhr

  • @hudi Neuester Build: jdk-9+175, kannst du das bestätigen?

    – Namann

    23. Juni 2017 um 1:45 Uhr

  • ja bauen: jdk-9+175

    – hudi

    23. Juni 2017 um 6:52 Uhr

Benutzer-Avatar
Nicolai Parlog

Das Problem ist, dass Ihr Modulpfad dasselbe Paket enthält (javax.annotation) in verschiedenen Modulen (java.xml.ws.annotation und tomcat.embed.core), was das Modulsystem verbietet, um Konfigurationen zuverlässiger zu machen. Dies wird als ein bezeichnet aufgeteiltes Paket. Das Modulsystem verrät es Ihnen, wenn es alle Module auflistet, die dieses Paket zweimal lesen (dh “sehen”). Was also jetzt tun?

Als erstes wäre zu prüfen, ob beide Pakete die gleichen Klassen enthalten. Wenn ja, haben Sie Glück. Jetzt müssen Sie nur noch sicherstellen, dass das Modulsystem nur eines davon sieht, wofür es zwei Möglichkeiten gibt:

  • Wenn Sie von einem der Module nur das eine Paket und sonst nichts benötigen, nehmen Sie es aus Ihrer Konfiguration und verwenden Sie stattdessen das andere. Vielleicht kannst du aufhören explizit zu verlangen java.xml.ws.annotation?
  • Wenn Sie setzen tomcat.embed.core auf dem Klassenpfad wird seine Version des Pakets vollständig ignoriert und das gesamte System, einschließlich des Codes auf dem Klassenpfad sieht nur das Paket herein java.xml.ws.annotation.

Wenn beide Varianten des Pakets Typen enthalten, die (a) die andere nicht enthält und (b) Ihre Anwendung benötigt, befinden Sie sich in einer schwierigeren Situation. Das erweckt zunächst einmal den Verdacht tomcat.embed.core etwas faul gemacht (obwohl ich mir da nicht sicher bin). Das einzige, von dem ich weiß, dass es helfen könnte, könnte sein nicht standardmäßig javac Möglichkeit --patch-module.

  • Versuchen Sie dies mit einem großen bestehenden Multi-Modul-Projekt und vielen Legacy-Abhängigkeiten. Viel Glück. Ich würde nur Java-Module verwenden, wenn ich ein neues Programm von Grund auf neu schreiben müsste.

    – Kuronashi

    23. Juni 2021 um 13:29 Uhr


Bei Spring Boot und eingebettetem Tomcat hatte ich ein ähnliches Problem. Es gibt ein Split-Paket, das in enthalten ist javax.annotation:javax.annotation-api sowie im org.apache.tomcat:tomcat-annotations-api.

javax.annotation:javax.annotation-api ist eine transitive Abhängigkeit von org.springframework.boot:spring-boot-starter-web.

Um dies für den speziellen Fall von Spring Boot und eingebettetem Tomcat zu beheben, war das Hinzufügen der Abhängigkeiten wie unten gezeigt die Lösung. Ich habe die Abhängigkeit von entfernt javax.annotation-api ausdrücklich. Das funktioniert, weil tomcat-annotations-api bietet auch alle erforderlichen Pakete und Klassen.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <!-- served by tomcat-annotations-api instead -->
                <groupId>javax.annotation</groupId>
                <artifactId>javax.annotation-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-annotations-api</artifactId>
    </dependency>

Benutzer-Avatar
Stefan

Ich habe folgenden Fehler:

[ERROR] Das Modul hibernate.core liest das Paket org.hibernate.dialect sowohl aus ParentProject als auch aus hibernate.core

beim Versuch, mvn clean install für mein Projekt auszuführen ParentProject. Das Auto generiert module-info.java zum ParentProject enthielt die Einträge

...
exports hibernate.core;
...
requires ChildProject;
...

Das module-info.java von ChildProject enthält

requires hibernate.core;

Entfernen der Linie exports hibernate.core; von dem ParentProject hat das Problem für mich gelöst.

=> Seien Sie vorsichtig mit der automatisch generierten module-info.java in Eclipse.

1014750cookie-checkSo beheben Sie den Modul liest Paketfehler in Java9

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy