Error:java: module A reads package B from both C and D on Java 11 project
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>
Please sign in to leave a comment.
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.
Hello Peter,
Does it compile fine using Maven from command line?
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.
Peter,
Could you please check the issue with 2018.3 release: https://www.jetbrains.com/idea/nextversion/ ?
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.
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
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: