web-dev-qa-db-fra.com

Comment résoudre une erreur de lecture de module dans Java9

J'essaie de comprendre la nouvelle modularité de Java 9 avec spring-boot. Je souhaite par exemple utiliser une application simple: https://github.com/tmatyashovsky/Java9-springboot

J'utilise maven 3.5.0 avec 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"

Le problème est que j'ai encore quelques exceptions. Qu'est-ce que cela signifie et comment dois-je résoudre le problème?

[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
19
hudi

Le problème est que votre chemin de module contient le même package (javax.annotation) dans différents modules (Java.xml.ws.annotation et Tomcat.embed.core), ce que le système de modules interdit afin de configurations plus fiables. Ceci s'appelle un paquet divisé . Le système de modules vous en dit autant lorsqu’il énumère tous les modules qui lisent (c'est-à-dire "voir") ce paquet deux fois. Alors que faire maintenant?

Le premier ordre du jour serait de vérifier si les deux paquets contiennent les mêmes classes. Si oui, vous avez de la chance. Il ne vous reste plus qu'à vous assurer que le système de modules n'en voit qu'un seul, pour lequel il existe deux possibilités:

  • Si vous utilisez l'un des modules, vous n'avez besoin que d'un seul paquet, retirez-le de votre configuration et utilisez l'autre. Peut-être que vous pouvez arrêter de demander explicitement Java.xml.ws.annotation?
  • Si vous mettez Tomcat.embed.core sur le chemin de classe, sa version du package sera complètement ignorée et l'ensemble du système, y compris le code du chemin de classe, ne verra que le package dans Java.xml.ws.annotation.

Si les deux variantes du package contiennent des types que (a) l'autre ne contient pas et (b) votre application a besoin, vous vous trouvez dans une situation plus difficile. Tout d’abord, cela laisse supposer que Tomcat.embed.core a fait quelque chose de louche (même si je n’en suis pas sûr). La seule chose que je sais qui pourrait aider pourrait être le non-standard javac option --patch-module .

12
Nicolai

Dans le cas de Spring Boot et de Tomcat intégré, j'ai rencontré un problème similaire. Il existe un package divisé, inclus dans javax.annotation:javax.annotation-api ainsi que dans org.Apache.Tomcat:tomcat-annotations-api.

javax.annotation:javax.annotation-api est une dépendance transitive de org.springframework.boot:spring-boot-starter-web.

Afin de résoudre ce problème dans le cas particulier de Spring Boot et de Tomcat intégré, l'ajout des dépendances illustrées ci-dessous constituait la solution. J'ai supprimé la dépendance de javax.annotation-api explicitement. Cela fonctionne car Tomcat-annotations-api fournit également tous les packages et classes requis.

    <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>
2
Erunafailaro

J'ai eu l'erreur suivante:

[ERROR] Le module hibernate.core lit le package org.hibernate.dialect de ParentProject et de hibernate.core.

en essayant d'exécuter mvn clean install pour mon projet ParentProject. Le module-info.Java généré automatiquement pour ParentProject contenait les entrées

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

Le module-info.Java de ChildProject contient

requires hibernate.core;

La suppression de la ligne exports hibernate.core; de la ParentProject a résolu le problème pour moi.

=> Faites attention avec le module généré automatiquement-info.Java dans Eclipse.

0
Stefan