java - com.google.inject 包可从多个模块访问

标签 java eclipse guice java-platform-module-system

我正在尝试将应用程序从 Java 8 升级到 Java 11.0.2。所以这些是我使用 Jigsaw 模块的第一步!

我的应用程序使用 Guice ,以及 AssistedinjectThrowingproviders 扩展。

这是我当前的module-info.java:

`

module com.example.mymodule {

    requires com.google.guice;
    requires com.google.guice.extensions.assistedinject;
    requires com.google.guice.extensions.throwingproviders;

    //...
}


`

该应用程序基于 Maven,当我运行 mvn package 时,我没有收到任何错误。但在 Eclipse (2018-12) 中,我遇到此错误“`The package com.google.inject isaccessible from more one module”:

error

enter image description here

我尝试在 module-info.java 中注释每个必需的模块,但我显然需要其中三个。

我可以做些什么来消除这个错误吗?或者这是一个 Eclipse 错误?

这是我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>example</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.7</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8.1</version>
        </dependency>
        <dependency>
            <groupId>commons-validator</groupId>
            <artifactId>commons-validator</artifactId>
            <version>1.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-text</artifactId>
            <version>1.6</version>
        </dependency>
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.11.3</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.26</version>
        </dependency>

        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <version>4.2.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.inject.extensions</groupId>
            <artifactId>guice-throwingproviders</artifactId>
            <version>4.2.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.inject.extensions</groupId>
            <artifactId>guice-assistedinject</artifactId>
            <version>4.2.2</version>
        </dependency>

    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                    <configuration>
                        <source>11</source>
                        <target>11</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
        </plugins>
    </build>
</project>

这是一个minimal project重现我的错误。

这是a video of the issue (为了清晰起见,请以 1080P 观看!)。

最佳答案

Eclipse 使用自己的编译器,在遵循规范方面甚至比 javac 更严格。您的模块信息中需要不同的模块/ jar ,它们都使用包 com.google.inject。这是 JPMS 规范中不允许的某种拆分包情况。 AFAIK javac 仅在不同模块的同一包中存在实际类时才会产生错误,但 eclipse 编译器在这里更加挑剔。

有一些解决方案可以解决拆分包问题。如果您找不到解决问题的较新版本的库(不幸的是,对于今天的大多数依赖项来说不太可能),您可以将模块合并为一个自定义模块,但这当然不是一个完美的解决方案。

有关该问题的更多背景信息另请参阅 Eclipse can't find XML related classes after switching build path to JDK 10https://bugs.openjdk.java.net/browse/JDK-8215739

关于java - com.google.inject 包可从多个模块访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54993710/

相关文章:

java - 我可以在 java axis2 客户端上捕获 https 的明文请求 SOAP 信封吗?

java - Eclipse 看不到 Java

java - 从 Guice 模块调用方法?

java - 类变量初始值设定项、类的静态初始值设定项和字段初始值设定项之间的区别

java - 为 customClass 列表定义方法

eclipse - 在 Eclipse 中使用 Pydev 自动完成时,如何保持可选关键字的持续显示?

linux - 如何在Eclipse中包含Linux内核开发的asm头目录?

java - Guice:在默认构造函数中使用注入(inject)的类作为局部变量

java - Guice CreationException 由于缺少 java.util.Optional 的实现

java - Activity.this 无法从静态上下文中引用