简体   繁体   中英

how to solve the Java upgrade 15 to 16 issue with Castor - XML

I have upgraded the java version from 15 to 16 and getting the below error on runtime.

java.lang.RuntimeException: Could not instantiate serializer com.sun.org.apache.xml.internal.serialize.XMLSerializer: java.lang.IllegalAccessException: class org.exolab.castor.xml.BaseXercesJDK5Serializer cannot access class com.sun.org.apache.xml.internal.serialize.XMLSerializer (in module java.xml) because module java.xml does not export com.sun.org.apache.xml.internal.serialize to unnamed module @646cb2e

It looks like you have encountered another manifestation of https://github.com/castor-data-binding/castor/issues/76 (closed).

This kind of thing is caused by application code (eg Castor) directly referring to internal JDK classes (in this case com.sun.org.apache.xml.internal.serialize.XMLSerializer ). This should be raised as an Castor issue.

Unfortunately, it looks like Castor data bindings is a moribund project, so you may need to develop your own fix to get it to work on Java 16 and later.

If you are going to do that, this is where the JDK internal package name is injected:

https://github.com/castor-data-binding/castor/blob/master/xml/src/main/java/org/exolab/castor/xml/

Look at XercesJDK5Serializer.java BaseXercesJDK5Serializer.java

You can set the property to use own Xerces to prevent the problem.

context.setProperty(org.castor.xml.XMLProperties.SERIALIZER_FACTORY,
    org.exolab.castor.xml.XercesXMLSerializerFactory.class.getName());

Change

Marshaller marshaller = new Marshaller(myWriter);

to something like this:

StringWriter myWriter= new StringWriter(); 
XMLContext xmlContext = new XMLContext();
xmlContext.setProperty(org.castor.xml.XMLProperties.SERIALIZER_FACTORY, 
    org.exolab.castor.xml.XercesXMLSerializerFactory.class.getName());
Marshaller marshaller = xmlContext.createMarshaller();
marshaller.setWriter(myWriter);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM