When migrating from Java 8 to Java 17 I began getting errors like this.
“The package org.w3c.dom.html is accessible from more than one module: , jdk.xml.
dom”
I think it has something to do with the introduction of modules in Java 9 so is not really a new problem.
I have reproduced the issue with a fairly small test case.
build.gradle
plugins {
id 'eclipse'
id 'java-library'
}
allprojects {
repositories {
mavenCentral()
}
}
dependencies {
api 'xerces:xercesImpl:2.12.2'
}
src/main/java/com/stuff/Thing.java
package com.stuff;
import org.apache.html.dom.HTMLDocumentImpl;
import org.w3c.dom.html.HTMLBodyElement; // Eclipse has the error here.
public class Thing {
public void operation() {
HTMLDocumentImpl htmlDocumentImpl = null;
HTMLBodyElement htmlBodyElement = null;
System.out.print("htmlDocumentImpl=" + htmlDocumentImpl);
System.out.print("htmlBodyElement=" + htmlBodyElement);
}
}
What is the solution to this issue? I think it has something to do with transitive dependencies but I have guessed at various solutions without success.
I’m using Gradle 7.3 and Eclipse 2022-06 (4.24.0) at the moment.
This is not so much a Gradle question, more a general Java question.
As the error tells you, Java 17 has the package org.w3c.dom.html in the module jdk.xml. dom: jdk.xml.dom (Java SE 17 & JDK 17)
And additionally the unnamed module has the package (empty string before the comma) which is because the package is also in the dependency xml-apis:xml-apis that is a dependency of your xerces:xercesImpl.
JPMS does not allow split packages (packages that are present in multiple modules) so it complains.
I’m not sure what the correct solution is in your case, but maybe you have to simply exlcude the xml-apis dependency.
I suppose it is more of a Java question than a Gradle question in terms of the concepts but my problem is that, although I think I understand the concepts somewhat, I cannot seem to figure out the correct Gradle incantation to make it happen. Here is an example of something I added which Gradle accepts but which seemed to have no impact on Eclipse.
Well, first question is, does it build properly from command line? If yes, the setup is fine. If it then does not work on the IDE, try refreshing. If it still does not work, try closing the project and then freshly importing it. If it still does not work, kind recommend using a better IDE. IntelliJ is imho far superior over Eclipse and it also has much better Gradle integration as far as I’m aware.
I think your last point about Eclipse may be the issue. I currently use a Gradle eclipse plugin and generate a .classpath and .project from the build.gradle. So I am not even using any Gradle integration Eclipse might have I suppose. I have not tried it.