java 如何解决java9中的模块读取包错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44697738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:18:08  来源:igfitidea点击:

How to resolve module reads package error in java9

javaspring-bootmavenjava-9module-path

提问by hudi

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

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

I am using maven 3.5.0 with java 9:

我在 java 9 中使用 maven 3.5.0:

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 Hymanson.databind reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module Hymanson.annotations reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module Hymanson.core reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module Hymanson.datatype.jdk8 reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module Hymanson.datatype.jsr310 reads package javax.annotation from both tomcat.embed.core and java.xml.ws.annotation
[ERROR] module Hymanson.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

回答by Nicolai

The problem is that your module path contains the same package (javax.annotation) in different modules (java.xml.ws.annotationand tomcat.embed.core), which the module system forbids in order to make configurations more reliable. This is called a split package. The module system tells you as much when listing all the modules that read (i.e. "see") that package twice. So what to do now?

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

First order of business would be to check whether both packages contain the same classes. If yes, you're in luck. Now all you need to do is make sure the module system only sees one of those, for which there are two possibilities:

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

  • If from one of the modules, you only need the one package and nothing else, take it out of your configuration and use the other one instead. Maybe you can stop explicitly requiring java.xml.ws.annotation?
  • If you put tomcat.embed.coreon the class path, its version of the package will be completely ignored and the entire system, including the code on the class pathwill only see the package in java.xml.ws.annotation.
  • 如果在其中一个模块中,您只需要一个包而不需要其他任何东西,请将其从配置中取出并改用另一个。也许您可以停止明确要求java.xml.ws.annotation
  • 如果你把tomcat.embed.core放在类路径上,它的包版本将被完全忽略,整个系统,包括类路径上的代码只会看到java.xml.ws.annotation 中的包。

If both variants of the package contain types that (a) the other does not contain and (b) your application needs, you're in a tougher situation. First of all, that raises the suspicion that tomcat.embed.coredid something fishy (although I'm not sure about that). The only thing I know could help could be the non-standard javacoption --patch-module.

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

回答by Erunafailaro

In case of Spring Boot and embedded Tomcat, I faced a similar issue. There is a split package, that is included in javax.annotation:javax.annotation-apias well as in org.apache.tomcat:tomcat-annotations-api.

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

javax.annotation:javax.annotation-apiis a transitive dependency of org.springframework.boot:spring-boot-starter-web.

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

In order to fix this for the particular case of Spring Boot and embedded Tomcat, adding the dependencies as shown below was the solution. I removed the dependency of javax.annotation-apiexplicitly. This works because tomcat-annotations-apiprovides all required packages and classes as well.

为了针对 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>

回答by Stefan

I got following error:

我收到以下错误:

[ERROR] module hibernate.core reads package org.hibernate.dialect from both ParentProject and hibernate.core

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

when trying to execute mvn clean install for my project ParentProject. The auto generated module-info.javafor ParentProjectcontained the entries

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

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

The module-info.javaof ChildProjectcontains

module-info.javaChildProject含有

requires hibernate.core;

Removing the line exports hibernate.core;from the ParentProjectresolved the issue for me.

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

=> Be careful with auto-generated module-info.java in Eclipse.

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