48

I am unable to import org.w3c.dom.NodeList package to Eclipse. It is showing

The package org.w3c.dom is accessible from more than one module: <unnamed>, java.xml" error message in eclipse.

Please let me know how to fix this ?

Eclipse Version:

Eclipse IDE for Enterprise Java Developers.

Version: 2019-06 (4.12.0)

Build id: 20190614-1200

Java version:

java version "12.0.1" 2019-04-16

Java(TM) SE Runtime Environment (build 12.0.1+12)

Java HotSpot(TM) 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
poovaraj
  • 609
  • 1
  • 6
  • 10
  • 7
    That is a limitation of Java 9 and higher: the same package name must not be used in more than one module. Everything on the classpath is considered as contained in the `` module. Since `org.w3c.dom` is already used in the system library, your code and all dependencies/JARs must not use this package even without using JPMS (without having a `module-info.java` file). So, either use Java 8 or get rid of the code/dependency that contains the `org.w3c.dom` package. – howlger Jul 31 '19 at 10:10
  • 2
    Possible duplicate of [Eclipse can't find XML related classes after switching build path to JDK 10](https://stackoverflow.com/questions/51094274/eclipse-cant-find-xml-related-classes-after-switching-build-path-to-jdk-10) – howlger Jul 31 '19 at 10:15
  • Thank you for the update. I am using java 12 version, so I cannot downgrade version. Please let me know how to create code/dependency that contains the org.w3c.dom package ? – poovaraj Jul 31 '19 at 15:29
  • 1
    Your code and everything on the classpath must not contain the `org.w3c.dom` package (as it already used in the system library). Maybe a newer version of the JAR containing `org.w3c.dom` has in newer versions its own name space for packages instead of `org.w3c.dom` so it can be used in Java 9 and higher. – howlger Jul 31 '19 at 21:57
  • Duplication https://stackoverflow.com/questions/51094274/eclipse-cant-find-xml-related-classes-after-switching-build-path-to-jdk-10 – Thirumal Jan 19 '20 at 16:03
  • @howlger How can I find out that a transitive dependency is using `org.w3c.dom` package? I have already excluded the dependencies: `stax-api`, `xml-apis` and `xercesImpl` as stated in other comments/answers but the errors remains. – ochs.tobi Apr 17 '20 at 10:20
  • @ochs.tobi The error message tells the package and you can use Java search (Ctrl+H) to find where the package is _declared_. By the way, your Eclipse is three releases behind. You might waste time by facing already fixed issues. – howlger Apr 17 '20 at 10:44
  • @howlger When I search for the declaration it tells me the import is from JDK11 -> java.xml. Can't see any other declaration. I don't have the issue in my IDE. I get the error from command line using maven. Maybe I should open a seperate question. – ochs.tobi Apr 20 '20 at 05:13
  • @ochs.tobi Yes, it's different when you get an error only in Maven but not in Eclipse. Maybe scopes are involved. If you open a question, tell which dependencies of which scope the package is contained, and if you have a `module-info.java` file. – howlger Apr 20 '20 at 06:37
  • @howlger Could you explain that a little more? *"the same package name must not be used in more than one module"* Is the implication that because Java itself already uses `org.w3c.dom.NodeList` internally somewhere, that means **i** cannot use it in my project? That seems like an unreasonable demand Java 9 is putting on developers. – Ian Boyd Jun 29 '22 at 19:45
  • @IanBoyd Yes exactly, that's what it means. I guess the reason for this new requirement is to make it easier for the HotSpot Java VM. – howlger Jun 30 '22 at 08:21
  • @howlger Something's not right. I have code that imports `java.lang`, and it doesn't get an error when it tries to use `String` or `Integer`. – Ian Boyd Jun 30 '22 at 14:27
  • @IanBoyd Importing is not a problem. But you are not allowed to have a `java.lang` plackage of your own. – howlger Jun 30 '22 at 15:19

17 Answers17

19

I had a similar issue because of a transitive xml-apis dependency. I resolved it using a Maven exclusion:

<dependency>
    <groupId>org.apache.xmlgraphics</groupId>
    <artifactId>fop</artifactId>
    <version>0.95</version>
    
    <exclusions>
        <exclusion>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Another dependency that just causes trouble and I don't have a solution other than removing it is this one:

<dependency>
    <groupId>com.oracle.database.xml</groupId>
    <artifactId>xmlparserv2</artifactId>
    <version>${oracle.version}</version>
</dependency>

Use mvn dependency:tree to see who brings in the transitive dependency, and then exclude that from there.

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
17

Disappointingly I don't see any compiler flags to show what jar the problem is with Even -Xlint:module doesn't seem to show up anything useful and eclipse doesn't shed any light on the issue

Instead to find where org.w3c.dom comes from I've been using this script:

mvn dependency:copy-dependencies -DincludeScope=test -DoutputDirectory=deps
for i in deps/*.jar; do if unzip -l $i| grep -q org.w3c.dom; then echo $i; fi ; done

Strictly you don't have to specify the scope test as that's the default but I've included it as you might want to use compile instead

wilx
  • 17,697
  • 6
  • 59
  • 114
MSillence
  • 553
  • 6
  • 8
9

On my side, I've spent a few hours to understand my issue, really closed to this one.
The package org.w3c.dom is accessible from more than one module: <unnamed>, java.xml

I wanted to migrate a project from Java 8 to Java 11. A few library issues. Easy to fix. But on this one,

  • I've tried to create module-info.java → it was worst.
  • Find a issue on my OS (debian 10) → even if Java 11 was default JRE, $JAVA_HOME was not rightly set for maven build. And when I was thinking it was only an Eclipse issue, I finally consider that it was a global compilation problem. To fix this I had to add following line in ~/.mavenrc

    JAVA_HOME=/usr/lib/jvm/default-java

  • Deep analysis on maven dependencies shows me a third-level dependency on xom.jar which trigger the issue. Dependency was linked to Saxon HE library → an upgrade to VERSION 9.9.X has resolved this boring problem.

Hope this will helps other people.

Damien C
  • 969
  • 1
  • 10
  • 16
6

In my case, it was caused by combining the usage of:

  • JDK 11
  • dom4j 2.1.3 library

As pointed out by others, the root cause is that dom4j and its dependencies (e.g., pull-parser) use some packages names (javax.xml.parsers, org.w3c.dom) that have been used by the JDK.

I had to remove dom4j to solve the problem. Just use JDK's own XML api.

zico
  • 139
  • 1
  • 3
4

In my case, culprit was Apache POI library. Added exclusion as below

<!-- Apache POI Library for Parsing Excel Sheets -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.0.0</version>
        <exclusions>
            <exclusion>
                <groupId>org.apache.xmlbeans</groupId>
                <artifactId>xmlbeans</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
Monish Sen
  • 1,773
  • 3
  • 20
  • 32
3

For Java 9 and higher Delete org.w3c.dom jar file from the class path, and you are done. By the way delete module info file too. You don't need to add the external jar file, its already included in the system library of java 9 and higher.

AsTeRiX
  • 31
  • 1
3

You can use this code (for example in a unit test) to determine all JARs that contain a class:

getClass().getClassLoader().getResources("org/w3c/dom/NodeList.class");

This gives you an Enumeration. The easiest way to print this is:

Collections.list(e).forEach(System.out::println);
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
2

Just open the configure build path and verify the modules which are all you have added as part of the project, which contains the class files as *

org.w3c.dom

This error, we usually gets in Java due to same kind of multiple API packages added in one project.

As, am using the same version as you mentioned, am not facing any issues., so just make sure that you don't have any duplicate modules.

2

org.w3c.dom is used in:

<dependency>
    <groupId>org.apache.xmlbeans</groupId>
    <artifactId>xmlbeans</artifactId>
    <version>2.4.0</version>
</dependency>

Check if this is imported transitively via some other dependency. Exclude the same

Add dependency for:

<dependency>
    <groupId>org.apache.xmlbeans</groupId>
    <artifactId>xmlbeans</artifactId>
    <version>2.6.0</version>
</dependency>
Sanjay Singh
  • 307
  • 1
  • 3
  • 13
1

In my case I was using:
JDK 14 and xmlbeans.jar library.
I just had to remove the xmlbeans.jar library and it surely solved the issue.

Peter Wauyo
  • 827
  • 1
  • 8
  • 11
1

In a Gradle land, you can track down which dependencies are contributing to xml-apis by running:

gradlew -q dependencies

(or core:dependencies for the core project in a multi-project environment).

In my case, xml-apis was being requested by both net.sourceforge.htmlunit and xom, so the solution is to exclude xml-apis in your build.gradle as so:

dependencies {
  implementation("net.sourceforge.htmlunit:neko-htmlunit:$nekoHtmlUnitVersion") {
    exclude group: "xml-apis"
  }
  testImplementation("xom:xom:$xomVersion") {
    exclude group: "xml-apis"
  }
  // ...other dependencies
}
jevon
  • 3,197
  • 3
  • 32
  • 40
1

For me the issue was:

<dependency>
        <groupId>com.monitorjbl</groupId>
        <artifactId>xlsx-streamer</artifactId>
        <version>2.2.0</version>
</dependency>

This package had a dependency on xml-api's.

I added this exclusion:

    <dependency>
        <groupId>com.monitorjbl</groupId>
        <artifactId>xlsx-streamer</artifactId>
        <version>2.2.0</version>
        <exclusions>
        <exclusion>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
        </exclusion>
    </exclusions>
    </dependency>
mgrecol
  • 41
  • 1
0

If you are in a simple Java Project, not Maven. Then just remove the dom-jaxb jar from the Libraries.

Steps : Right click on project -> Properties -> Java BuildPath -> Libraries Tab -> select on jars such as dom-2.3.0-jaxb-1.0.6(version might differ) -> Remove.

Now it will build without error.

The error is occurring because "org.w3c.dom.Document" is coming from both the removed "dom-2.3.0-jaxb-1.0.6" jar and from Java's in-built libraries. Removing additional jar will let it come only from Java's in-built libraries.

0

You need to manually check and remove all the jars (libraries) which has the possibility of conflicting with java.xml package.

In my case, I edited my .classpath file and removed the following jars and it resolved the issue:

jtidy.jar, castor-0.9.5.4-xml.jar, xercesImpl.jar, xml-apis.jar
RajSanpui
  • 11,556
  • 32
  • 79
  • 146
0

Sometimes even when you have removed duplicate classes the same error "The package org.w3c.dom is accessible from more than one module" may remain, especially with IDEs.

IT IS NOT always enought to find only org.w3c.dom!!!! You may need to remove duplicate classes which share the same parent package org.w3c.xxx, for example org.w3c.css

We managed to fix it like this:

  • In IDE search classes starting with: org.w3c
  • Add pom.xml exclusions or increase library version, since org.w3c has been removed in some newer lib versions
  • Test compile in both IDE and command line

If the issue persists:

  • Export all dependencies to file: mvn dependency:copy-dependencies -DincludeScope=test -DoutputDirectory=deps
  • From jar-files find any classes of org.w3c
  • Keep adjusting pom.xml
Jukka
  • 406
  • 4
  • 6
0

I am getting an error when I add the class DocumentBuilderFactory to my maven project. After investing many hours I get a fixed solution that is related to maven dependency.

I see the same javax.xml is present in the many jar file. so I make it exclude.

for exclude the jar file from maven dependency --->

  1. right click on jar file
  2. click on maven
  3. use see the open exclude click on that,it gets exculde.

check-in your pom.xml file. see my below code for your ref.

<dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.10-FINAL</version>
        <exclusions>
            <exclusion>
                <artifactId>xml-apis</artifactId>
                <groupId>xml-apis</groupId>
            </exclusion>
            <exclusion>
                <groupId>dom4j</groupId>
                <artifactId>dom4j</artifactId>
            </exclusion>
            <exclusion>
                <groupId>stax</groupId>
                <artifactId>stax-api</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.apache.xmlbeans</groupId>
                <artifactId>xmlbeans</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
  • above code is just fix my issue but due to above the changes create new issue like testsuite not working. so its fix solution is found is to increase verison of poi-ooxml to 3.17. It fix my all issues. – lalit dhend Mar 04 '23 at 10:56
0

For me the issue was with dom4j.2.1.3 dependency. I replaced it with latest dom4j dependency

<dependency>
    <groupId>org.dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>2.1.4</version>
</dependency>

Also, made some changes in code to remove org.w3c.dom dependency of jdk11 and replace below methods like

Document doc = dBuilder.parse(inputFile); ===> Document doc = reader.read(inputFile);

doc.getDocumentElement().normalize(); ===> doc.getDocument().normalize();

NodeList nList = doc.getElementsByTag("STOCKITEM") ===> List nList = doc.selectNodes("STOCKITEM");

String closingQty = eElement .getElementsByTagName("CLOSINGQTY") .item(0).getTextContent(); ===> String closingQty = eElement.selectSingleNode("CLOSINGQTY").getText();

Parag
  • 11
  • 2