Eclipse Unable to Locate XML Classes Post JDK 10 Build Path Transition: Resolution
When attempting to switch a Maven project's build path to JDK 10 within Eclipse, users may encounter an issue where Eclipse build processes fail to locate XML-related classes, including javax.xml.xpath.XPath, org.w3c.dom.Document, and org.xml.sax.SAXException. These difficulties primarily affect classes derived from the xml-apis-1.4.01 dependency.
Root Cause
The behavior is triggered when a project transitions from Java 1.8 to Java 10 without being explicitly declared as a module using a module-info.java file. This means that the project's code is compiled in the unnamed module, which has full visibility of named named and unnamed modules, including java.xml from the JRE System Library, which exports these problematic packages.
Additionally, xml-apis.java on the classpath provides another set of these same packages. However, these packages are associated with the unnamed module, leading to a violation of the "unique visibility" requirement as defined in JLS §7.4.3. Consequently, the program becomes invalid and faces rejection from compilers.
Solution
To resolve this issue, one of three approaches can be adopted:
(1) Declare Project as a Module
Introduce a module-info.java file within the project, defining which specific modules the project should read through the use of the "requires" keyword. This approach ensures unique visibility by explicitly stating which packages are imported.
(2) Limit Observable Modules
Without converting the project to a module, it is possible to exclude java.xml from the set of observable modules. In Eclipse, this can be accomplished through the "Modularity Details" dialog, under the "Contents" tab. It may be necessary to push most modules apart from java.base to the "Available modules" column and selectively re-add essential ones.
(3) Exclude java.xml (JDK Bug)
This approach is less recommended, as it takes advantage of a bug in javac that allows the illegal situation to pass compilation. By moving java.xml to the end of the build path, it can be effectively hidden from the unnamed module. However, this may lead to unexpected behavior and is not a reliable long-term solution.
In conclusion, the XML-related class not-found issue when switching to JDK 10 in Eclipse is caused by a violation of the unique visibility requirement. The issue is resolved by either modularizing the project or limiting the observable modules.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3