34

I am trying to understand new modularity in java 9 with spring-boot so I want to run some simple application for example: https://github.com/tmatyashovsky/java9-springboot

I am using maven 3.5.0 with 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"

Problem is I still got some exception. What does it mean and how should I fix it ?

[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
4

3 回答 3

26

问题是您的模块路径javax.annotation在不同的模块(java.xml.ws.annotationtomcat.embed.core)中包含相同的包( ),模块系统禁止这样做以使配置更可靠。这称为拆分包。当列出两次读取(即“看到”)该包的所有模块时,模块系统会告诉您同样多的信息。那么现在该怎么办呢?

首要任务是检查两个包是否包含相同的类。如果是的话,你很幸运。现在你需要做的就是确保模块系统只看到其中之一,有两种可能性:

  • 如果来自其中一个模块,您只需要一个包而无需其他任何东西,请将其从您的配置中取出并使用另一个包。也许您可以停止明确要求​​ java.xml.ws.annotation
  • 如果将tomcat.embed.core放在类路径上,它的包版本将被完全忽略,整个系统,包括类路径上的代码,只会看到java.xml.ws.annotation中的包。

如果包的两个变体都包含 (a) 另一个不包含的类型和 (b) 您的应用程序需要的类型,那么您的处境将更加艰难。首先,这让人怀疑tomcat.embed.core做了一些可疑的事情(尽管我不确定)。我知道唯一可以帮助的可能是非标准javac选项--patch-module

于 2017-06-30T08:35:13.400 回答
6

在 Spring Boot 和嵌入式 Tomcat 的情况下,我遇到了类似的问题。有一个拆分包,它包含javax.annotation:javax.annotation-apiorg.apache.tomcat:tomcat-annotations-api.

javax.annotation:javax.annotation-api是 的传递依赖org.springframework.boot:spring-boot-starter-web

为了针对 Spring Boot 和嵌入式 Tomcat 的特定情况解决此问题,添加如下所示的依赖项是解决方案。javax.annotation-api我明确地删除了依赖。这是有效的,因为tomcat-annotations-api它也提供了所有必需的包和类。

    <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>
于 2017-11-08T07:06:45.560 回答
1

我收到以下错误:

[错误] 模块 hibernate.core 从 ParentProject 和 hibernate.core 读取包 org.hibernate.dialect

尝试为我的项目执行 mvn clean install 时ParentProject。自动生成module-info.javaParentProject包含条目

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

的包含module-info.java_ChildProject

requires hibernate.core;

exports hibernate.core;从解决方案中删除该行为ParentProject我解决了问题。

=> 小心在 Eclipse 中自动生成的 module-info.java。

于 2017-11-11T15:50:14.110 回答