如何在java9中解析模块读取包错误

时间:2017-06-22 11:13:08

标签: java maven spring-boot java-9

我试图通过spring-boot来理解java 9中的新模块化,所以我想运行一些简单的应用程序,例如:https://github.com/tmatyashovsky/java9-springboot

我正在使用maven 3.5.0和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"

问题是我还有一些例外。它是什么意思,我应该如何解决它?

[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

3 个答案:

答案 0 :(得分:13)

问题是你的模块路径在不同的模块中包含相同的包(javax.annotation)( java.xml.ws.annotation tomcat.embed.core ),模块系统禁止使配置更可靠。这称为split package。模块系统在列出所有读取的模块(即"参见")两次包时,会告诉您。那现在该怎么办?

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

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

如果包的两个变体都包含(a)另一个不包含的类型和(b)您的应用程序需求,那么您处于更加困难的境地。首先,这引起了人们的怀疑,即 tomcat.embed.core 做了一些可疑的事情(尽管我对此并不确定)。我知道的唯一可以帮助的可能是non-standard javac option --patch-module

答案 1 :(得分:2)

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

javax.annotation:javax.annotation-apiorg.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>

答案 2 :(得分:0)

我收到了以下错误:

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

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

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

module-info.java的{​​{1}}包含

ChildProject

requires hibernate.core; 删除行exports hibernate.core;为我解决了问题。

=&GT;注意Eclipse中自动生成的module-info.java。