Error:java: module A reads package B from both C and D on Java 11 project

Answered

I got the following errors when build project using IntelliJ IDEA 2018 2.5 Community after migrating to Java 11 (I got this error on a lot of modules)
Error:java: the unnamed module reads package javax.xml.stream from both java.xml and stax.api
Error:java: module org.openqa.selenium.remote reads package org.hamcrest from both hamcrest.all and hamcrest.core

Cause of this error is that %USERPROFILE%\.m2\repository\stax\stax-api\1.0.1\stax-api-1.0.1.jar and %USERPROFILE%\.m2\repository\repository\javax\xml\stream\stax-api\1.0-2\stax-api-1.0-2.jar contains the same java.xml.stream package
Same with %USERPROFILE%\.m2\repository\org\hamcrest\hamcrest-all\1.3\hamcrest-all-1.3.jar and org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar

Here is my module-info.java

module Common {

requires commons.io;
requires org.openqa.selenium.core;
requires org.openqa.selenium.chrome;
requires testng;
requires selenium.support;
requires java.datatransfer;
requires java.desktop;
requires java.sql;
requires commons.lang;
requires twilio;
requires hamcrest.all;
requires commons.csv;
requires org.openqa.selenium.ie;
requires json;
requires java.xml;

}

If I enter Project settings and delete Maven: stax:stax-api:1.0.1 and Maven: javax.xml.stream:stax-api:1.0-2 i File - Project Structure Module & Library
It works but every time time pom.xml is reimported it stops working.
Tried to update the dependencys to the latest version.

Tried following:

1. In settings as java compiler compilation oprtions added --add-module okhttp3 --patch-module okhttp3=C:\Users\soapui\.m2\repository\xml-apis\xml-apis\1.4.01\xml-apis-1.4.01.jar

2. Exclude in maven-compiler-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>11</source>
<target>11</target>
<excludes>
<exclude>javax.xml.stream</exclude>
<exclude>stax</exclude>
</excludes>
</configuration>
</plugin>

3. Added exclusions in all dependencys
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M2</version>
<executions>
<execution>
<id>enforce</id>
<configuration>
<rules>
<DependencyConvergence/>
</rules>
</configuration>
<goals>
<goal>enforce</goal>
</goals>
<phase>validate</phase>
</execution>
</executions>
</plugin>
...
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20171018</version>
<exclusions>
<exclusion>
<groupId>javax.xml.stream</groupId>
<artifactId>stax-api</artifactId>
</exclusion>
</exclusions>
</dependency>

4. Banned classes
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M2</version>
<executions>
<execution>
<id>enforce</id>
<configuration>
<rules>
<banDuplicateClasses>
<ignoreClasses>
<ignoreClass>javax.xml.*</ignoreClass>
<ignoreClass>javax.xml.namespace.*</ignoreClass>
<ignoreClass>javax.xml.stream.*</ignoreClass>
<ignoreClass>javax.xml.stream.util.*</ignoreClass>
<ignoreClass>javax.xml.stream.events.*</ignoreClass>
<ignoreClass>javax.xml.namespace.*</ignoreClass>
<ignoreClass>org.hamcrest.*</ignoreClass>
<ignoreClass>org.hamcrest.internal.*</ignoreClass>
<ignoreClass>org.hamcrest.internal.core.*</ignoreClass>
</ignoreClasses>
<findAllDuplicates>true</findAllDuplicates>
<ignoreWhenIdentical>true</ignoreWhenIdentical>
</banDuplicateClasses>
</rules>
<fail>true</fail>
</configuration>
<goals>
<goal>enforce</goal>
</goals>
<phase>validate</phase>
</execution>
</executions>
</plugin>

5. Added compiler args
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>11</source>
<target>11</target>
<compilerArgs>
<arg>--add-modules</arg>>
<arg>org.openqa.selenium.remote</arg>
<arg>--patch-module</arg>
<arg>org.openqa.selenium.remote=C:\Users\soapui\.m2\repository\org\hamcrest\hamcrest-all\1.3\hamcrest-all-1.3.jar</arg>
</compilerArgs>
</configuration>
</plugin>

 

0
7 comments
Avatar
Permanently deleted user

I don't think this is the IDEA specific error.

I had similar problem with the gradle project which was eventually solved by adding exclusions into test dependencies:

testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.hamcrest', module: 'hamcrest-core'
exclude group: 'org.skyscreamer', module: 'jsonassert'
}

Get the list of dependencies and see where exactly to put those exclusions as it might be tricky, depending on the project.

0

Hello Peter,

Does it compile fine using Maven from command line?

0
Avatar
Permanently deleted user

I use Maven so I cannot use the Gradle exclusions. I have tried a lot of <exclusion> in <dependency> in pom.xml without success.

Yes I can mvn clean install from the Maven context menu inside IntelliJ and build succeeds.

0

Peter,

Could you please check the issue with 2018.3 release: https://www.jetbrains.com/idea/nextversion/ ?

 

0
Avatar
Permanently deleted user

Maven dependencies working in the similar fashion.

The key here is to get the dependency tree and find all paths to the modules that build is complaining about. Then exclude one or the other whichever makes more sense. It could be tricky though.

1
Avatar
Permanently deleted user

I think I solved the problem. At least project compile. Have not tested it at runtime yet

I solved the problem by downloading the Maven Helper Plugin.

Click on pom.xml and click on the Dependency analyzer tab, Select All dependencies as List.

Select stax-api 1.0.1 & stax-api 1.0.2 and Exclude

0

Big Thanks Peter Landhage !! you made my day. I have similar issue, following your trick it is now working like a charm.

Still I think that there is some strange behavior in this. For example I had similar issue with module from geronimo-jta_1.1_spec and geronimo-ws-metadata_2.0_spec so I correctly excluded them in pom file:

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>${cxf.version}</version>
<exclusions>
<exclusion>
<artifactId>geronimo-jta_1.1_spec</artifactId>
<groupId>org.apache.geronimo.specs</groupId>
</exclusion>
<exclusion>
<artifactId>geronimo-ws-metadata_2.0_spec</artifactId>
<groupId>org.apache.geronimo.specs</groupId>
</exclusion>
</exclusions>
</dependency>

And this was OK for Maven, but not for IDEA. I have to manually exclude them like Peter said. So that means to me that dependency exclusion in Maven and module resolution in Idea still have some glitches...

 

0

Please sign in to leave a comment.