score: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.

score: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

score:0

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);

score:0

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
}

score: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.

score: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.

score: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>

score: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.

score:3

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>

score:5

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.

score:8

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.

score:14

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

score:16

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.


Related Query

More Query from same tag