score:99

Accepted answer

i assume that the project being migrated from java 1.8 still has no module-info.java. this implies you are compiling code in the "unnamed module".

code in the unnamed module "reads" all observable named and unnamed modules, in particular it reads module "java.xml" from the jre system library. this module exports package like java.xml.xpath.

additionally, you have xml-apis.java on the classpath, which contributes another set of packages of the same names (java.xml.xpath and friends). these are said to be associated to the unnamed module, like your own code.

this situation violates the requirement of "unique visibility" as defined in jls §7.4.3 (last paragraph). in particular every qualified type name q.id (jsl §6.5.5.2) requires that its prefix q is a uniquely visible package (i'm disregarding the case of nested types for simplicity). ergo: the program is illegal and must be rejected by compilers.

this leaves us with one question and two solutions:

(1) question: why is javac accepting the program?

(2) solution: if you add module-info.java to your project, you can control via requires which module your project reads, either requires java.xml; or requires xml.apis; (where "xml.apis" is the automatic module name of "xml-apis-1.4.01.jar).

(3) solution: short of turning your project into a module, you can still avoid the conflict by excluding java.xml from the set of observable modules. on the command line this would be done using --limit-modules. the equivalent in eclipse is the "modularity details" dialog, see also the jdt 4.8 new&noteworthy (look for contents tab). since java.xml is implicitly required via a lot of other default-observable modules, it may be a good idea to push everything except for java.base from right ("explicitly included modules") to left ("available modules") (and selectively re-add those modules that your project needs).

ps: eclipse still doesn't provide an ideal error message, instead of "cannot be resolved" it should actually say: "the package javax.xml.xpath is accessible from more than one module: javax.xml, <unnamed>.

pps: also weird: how come that changing the order between jre and a jar on the classpath (such ordering is not a concept supported by javac nor jep 261) changes the behavior of the compiler.

edits:

  • alex buckley confirmed that the given situation is illegal, despite what javac says. bug against javac has been raised as jdk-8215739. this bug has been acknowledged months before the release of java 12. as of 2019-06 it has been decided that also java 13 will ship without a fix. similarly for java 14. the bug was temporarily scheduled for java 15, but this plan has been dropped on 2020-04-20.
  • eclipse error message has been improved to mention the real problem.
  • in eclipse 2019-06 the ui used for solution (3) has been revamped. up-to-date documentation can be found in the online help.

score:0

jdk 9+ brought in changes related to project jigsaw. jdk was broken down into various modules and some modules, javaee, jaxb and xml related, are no more loaded by default. you should add these to your maven build directly, instead of expecting them to be in jre classpath. see this so question

score:1

this is more of a work-around, but from my experience it can be resolved by going to the "java build path", the "order and export" tab, and sending the "maven dependencies" to the bottom (so it's below the "jre system library").

score:2

thanks for this clue. i was having trouble identifying where the conflicting reference was coming from for org.w3c.dom.document. found it easily in eclipse 2020-12 this way: selected org.w3c.dom.document within the import statement that eclipse flagged, right-click and choose open type hierarchy, in the type hierarchy dialog right click document at the top and choose implementors > workspace to reveal all the jars in all projects in the workspace which are bringing in org.w3c.dom.document (or whatever type you have selected that is accessible from more than one module – mikeonline yesterday

following the directions above from one of the earlier posts helped us solve our issue. what we did was replace document with genericdocument and element with genericelement from batik - and compile errors are gone - now we just have to test to make sure the implementation matches what we had under java 8. thanks mikeonline

score:3

what happens here is you have a wildcard import like import org.w3c.dom.*, stating you want to import all classes from package org.w3c.dom. now, if there's at least one class in org.w3c.dom provided by a second source, java must not start (as pointed out here).

(by the way, the message "... cannot be resolved" is replaced by a more accurate error message "the package org.w3c.dom is accessible from more than one module: <unnamed>, java.xml" in more recent eclipse versions, see this merged change request by stephan herrmann.)

to resolve this problem

  1. open the "open type" dialog (ctrl+shift+t).
  2. enter the complete import, so org.w3c.dom.* or org.w3c.dom..
  3. check the entire list for multiple sources. all entries here should contain only something like "jdk-11-...".
  4. gather all jars that contain classes you have multiple sources for.
  5. open the "dependency hirarchy" tab from pom.xml.
  6. search for the jar file.
  7. add an exlusion (right click or edit the pom.xml manually).

example

i had this findbugs dependency in my pom.xml:

<dependency>
    <groupid>com.google.code.findbugs</groupid>
    <artifactid>findbugs</artifactid>
    <version>${findbugs.version}</version>
</dependency>

findbugs has two dependencies that need to be excluded:

<dependency>
    <groupid>com.google.code.findbugs</groupid>
    <artifactid>findbugs</artifactid>
    <version>${findbugs.version}</version>
    <exclusion>
        <groupid>xml-apis</groupid>
        <artifactid>xml-apis</artifactid>
    </exclusion>
    <exclusion>
        <groupid>jaxen</groupid>
        <artifactid>jaxen</artifactid>
    </exclusion>
</dependency>

score:4

have seen something very similar under eclipse 4.8.0 and jdk 10. e.g.

import org.w3c.dom.element;

was failing to compile in eclipse with: the import org.w3c.dom.element cannot be resolved

even so, pressing f3 (open declaration) on that import, eclipse was able to open the interface definition - in this case under xml-apis-1.4.01.jar.

meanwhile, builds from maven direct were working fine.

in this case the fix was to remove this dependency from the pom.xml:

    <dependency>
        <groupid>xml-apis</groupid>
        <artifactid>xml-apis</artifactid>
        <version>1.4.01</version>
    </dependency>

then the compile errors in eclipse melted away. following f3 again showed the element interface - now under the java.xml module, under the jre system library under the project. also the maven build remained fine.

this feels like a problem with eclipse resolving a class that it finds in both a jdk module and dependent .jar file.

interestingly, in a separate environment, this time under eclipse 4.9.0 and jdk 11, all is fine, with or without the xml-apis:1.4.01 dependency.

score:4

while stephan herrmann's answer is the correct one, i'll post my error and how i got it solved if it can help others. i had the error the package javax.xml.namespace is accessible from more than one module: <unnamed>, java.xml and after inspecting the class with the error, it was the javax.xml.namespace.qname import that was complaining. with the "open type" dialog, i found out that it was pulled from stax-api through eureka client. this solved it for me :

<exclusion>
   <groupid>stax</groupid>
   <artifactid>stax-api</artifactid>
</exclusion>

score:5

this seems to have been reported as eclipse bug 536928. maybe if everyone were to go vote on it it would get them to raise the priority.

score:11

in my case the problem was that xercesimpl : 2.10.0 was a (transient) dependency. this jar bundles org.w3c.dom.html.htmldomimplementation.

as far as i understand the org.w3c.dom package then becomes available from two modules, causing the build to fail. in case one of the dependencies (direct or transient) has classes in one of the 25 packages exported by the java.xml module your build will fail.

excluding xercesimpl (and also the offenders listed below) in maven solved the issue for me:

    <dependency>
        <groupid>xyz</groupid>
        <artifactid>xyz</artifactid>
        <version>1.0</version>
        <exclusions>
            <exclusion>
                <groupid>xerces</groupid>
                <artifactid>xercesimpl</artifactid>
            </exclusion>
            <exclusion>
                <groupid>xml-apis</groupid>
                <artifactid>xml-apis</artifactid>
            </exclusion>
            <exclusion>
                ...
            </exclusion>
        </exclusions>
    </dependency>

thanks to rune flobakk for giving the hint here: https://bugs.eclipse.org/bugs/show_bug.cgi?id=536928#c73

other offenders:

  • batik-ext : 1.9 (bundles org.w3c.dom.window)
  • xom : 1.2.5 (bundles org.w3c.dom.userdatahandler)
  • stax-api : 1.0.2 (bundles javax.xml.stream.eventfilter)
  • xml-apis : 1.4.01 (bundles org.w3c.dom.document)
  • xml-beans : 2.3.0 (bundles org.w3c.dom.typeinfo)

Related Query

More Query from same tag