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

Community
  • 1
  • 1
user3820339
  • 151
  • 1
  • 4
  • 11
  • @AlanBateman are they(classes from those packages) getting patched? – Naman Dec 11 '18 at 14:00
  • 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? – Alan Bateman Dec 11 '18 at 14:44
  • @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 ? – user3820339 Dec 12 '18 at 07:46
  • 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. – Alan Bateman Dec 12 '18 at 10:11
  • 1
    https://bugs.eclipse.org/bugs/show_bug.cgi?id=536928 – user3820339 Dec 12 '18 at 15:47
  • @user3820339 **you** can help resolving this: by providing a reproducing example. I tried a lot of variants -- all seem to work as designed. – Stephan Herrmann Dec 12 '18 at 23:46
  • @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. – user3820339 Dec 13 '18 at 06:46
  • See my answer in the other thread: https://stackoverflow.com/a/53824670/4611488 – Stephan Herrmann Dec 18 '18 at 00:11
  • @StephanHerrmann I looked your comment at stackoverflow.com/a/53824670/4611488 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. – user3820339 Dec 18 '18 at 06:59
  • @user3820339 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. – Stephan Herrmann Dec 18 '18 at 17:08
  • 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. – greg-449 Dec 19 '18 at 15:09
  • Is this the problem that was resolved via https://bugs.eclipse.org/542896 ? – Stephan Herrmann Jan 22 '19 at 22:39
  • https://stackoverflow.com/questions/55905047/dependency-not-resolved-documentbuilderfactory-class-need-dependency-of-javax-x/57987867#57987867 – marknovemg Sep 18 '19 at 08:15

1 Answers1

2

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.

Philip Dilip
  • 181
  • 2
  • 4