XML-api.jar classes having issues with JAVA 11 in EClipse 4.9
Asked Answered
L

1

7

org.xml.sax and org.w3c.dom classes are not working with JAVA 11 and Eclipse Photon 4.9 & 4.10 RC2 build

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

import org.xml.sax.DocumentHandler;
import org.xml.sax.ErrorHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.AttributeList;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;

enter image description here

enter image description here

These class imports from the org.w3c.dom & org.xml.sax are not working with Eclipse SDK

Version: 2018-09 (4.9) Build id: I20180906-0745

Problem Description :

Post import of the project in eclipse workspace (Downloaded the JAVA 11 compatible Eclipse 4.9 version.), this start giving error during compilation for these classes.

This error goes away if you simply use

import org.xml.sax.*;
import org.w3c.dom.*; 

instead of usual complete package name. But this only gets away the error but doesn't help completely. So If we go further and want to use and import any particular specific classes from the sax or w3c , it will not give option and you cannot even make explicit import. SO this approach is of no use.

Another workaround is, if we place the JRE System Library (JAVA 11) above in order & export section of configure buildpath option of ECLIPSE workspace, then these import errors go away.

xml-apis.jar

But this is only happening if we compile using the JAVA 11 through compiler of Eclipse. If we switch back to JAVA 8 compiler in eclipse then same combination works fine.

This issue happens with JAVA 9 also.

Could someone please help resolving this?

There is already an Eclipse bug filed but it has not yet any resolution : https://bugs.eclipse.org/bugs/show_bug.cgi?id=536928#c12

enter image description here

Leukocyte answered 11/12, 2018 at 9:55 Comment(13)
@AlanBateman are they(classes from those packages) getting patched?Luxate
I'm not sure what you by "getting patched". Are you asking if the W3C DOM APIs and SAX APIs are supported? Yes. Or maybe you mean that the question involves someone patching the java.xml module with ancient or alternative API classes?Serbocroatian
@AlanBateman .This issue of org.xml.sax api and org.w3c.dom api is only coming after switching the compiler to JAVA 11 or JAVA 9 through eclipse 4.9 build. There is not any issue observed with JAVA 8 compiler setting in eclipse for the same project and classes, So, I think this is more of an Eclipse issue that why it is failing to recognize and load the packages from xml-api.jar ?Leukocyte
The java.xml module exports these APIs and that is where the Eclipse compiler should find them. It should not be looking in xml-api.jar for these API classes.Serbocroatian
bugs.eclipse.org/bugs/show_bug.cgi?id=536928Leukocyte
@Leukocyte you can help resolving this: by providing a reproducing example. I tried a lot of variants -- all seem to work as designed.Geodesic
@StephanHerrmann Steps:- 1. Create a java project and add a simple HelloWorld.java class file 2. Open the HelloWorld.java class file and add the following imports to that :- import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; then save it. you will see the compilation error. Most important thing is to put the xml-apis.jar higher in order than java 11 in order & export section of configure build path.Leukocyte
See my answer in the other thread: https://mcmap.net/q/94565/-eclipse-can-39-t-find-xml-related-classes-after-switching-build-path-to-jdk-10Geodesic
@StephanHerrmann I looked your comment at https://mcmap.net/q/94565/-eclipse-can-39-t-find-xml-related-classes-after-switching-build-path-to-jdk-10 but I couldn't get my answer. we are having xm-apis.jar import higher than JDK 11 in our project path. Please help me if I am missing on anything. Thanks for your help.Leukocyte
@Leukocyte according to my understanding of JLS since Java 9 the order of elements on the build path should not be relevant at all. Having two modules serving the same package, when both are accessible in your code is an error. Also code on a classpath lives now in a module: an unnamed module.Geodesic
Note that Eclipse 4.9 and 4.10 are not Eclipse Photon, they are 'SimRel 2018-09' and 'SimRel 2018-12' respectively. Photon is 4.8 only.Ninette
Is this the problem that was resolved via bugs.eclipse.org/542896 ?Geodesic
#55905547Erose
F
4

I don't think this issue is specific to Eclipse IDE.

Java 11 now has it's own xml package which include all XML Parsing API's which are present at external libraries like "xml-api".

Thus, if you are using such external APIs related to XML, Java compiler finds multiple sources of the same class and hence unable to resolve them.

I excluded all xml-api artifacts from the dependencies defined at POM, which had been pulling "xml-api" artifacts from maven repositories.

USE

mvn dependency:tree -Dverbose -Dincludes xml-api:xml-api

To trace the artifacts responsible for pulling XML apis and exclude them.

I was able to resolve all such compilation errors in my maven project.

Java 8 had not packaged XML APIs in its System library, and hence you haven't observed any issues after switching back to Java 8.

Fireman answered 4/9, 2022 at 20:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.