Search code examples
javaeclipseguicejava-platform-module-system

The package com.google.inject is accessible from more than one module


I'm trying to upgrade an application to Java 11.0.2, from Java 8. So those are my very first steps with Jigsaw modules!

My application uses Guice, and the Assistedinject, and Throwingproviders extensions.

Here's my current module-info.java:

`

module com.example.mymodule {

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

    //...
}


`

The application is based on Maven and when I run mvn package I get no error. But In Eclipse (2018-12), I have this error "`The package com.google.inject is accessible from more than one module":

error

enter image description here

I tried commenting each of the required module in module-info.java but I clearly need the three of them.

Is there something I can do to remove this error? Or is this an Eclipse bug?

Here's my 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>

Here is a minimal project to reproduce my error.

And here's a video of the issue (to be watched in 1080P for clarity!).


Solution

  • Eclipse uses its own compiler which is even stricter than javac in following the specs. You are requiring different Modules/Jars in your module-info which are all using the package com.google.inject. This is some kind of a split package situation which is not allowed in the JPMS spec. AFAIK javac only yields an error if there are actual classes in the same packages of different moduls, but the eclipse compiler is even more picky here.

    There are some solutions out there for solving split package problems. If you don't find a newer version of the libs where the problem is solved (which unfortunately is not very likely for most dependencies today) you could f.e. merge the modules into one custom modules, but this is not a perfect solution of course.

    For more background information on the issue see also Eclipse can't find XML related classes after switching build path to JDK 10 and https://bugs.openjdk.java.net/browse/JDK-8215739