简体   繁体   English

如何使用 Castor - XML 解决 Java 升级 15 到 16 的问题

[英]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 版本从 15 升级到 16,并在运行时出现以下错误。

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 java.lang.RuntimeException:无法实例化序列化程序 com.sun.org.apache.xml.internal.serialize.XMLSerializer:java.lang.IllegalAccessException:类 org.exolab.castor.xml.BaseXercesJDK5Serializer 无法访问类 com.sun.org .apache.xml.internal.serialize.XMLSerializer(在模块 java.xml 中)因为模块 java.xml 不会将 com.sun.org.apache.xml.internal.serialize 导出到未命名模块 @646cb2e

It looks like you have encountered another manifestation of https://github.com/castor-data-binding/castor/issues/76 (closed).看来您遇到了https://github.com/castor-data-binding/castor/issues/76的另一种表现形式(已关闭)。

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 ).这种事情是由应用程序代码(例如 Castor)直接引用内部 JDK 类(在本例中为com.sun.org.apache.xml.internal.serialize.XMLSerializer )引起的。 This should be raised as an Castor issue.这应该作为 Castor 问题提出。

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.不幸的是,Castor 数据绑定似乎是一个垂死的项目,因此您可能需要开发自己的修复程序以使其在 Java 16 及更高版本上运行。

If you are going to do that, this is where the JDK internal package name is injected:如果你要这样做,这里就是注入 JDK 内部包名的地方:

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

Look at XercesJDK5Serializer.java BaseXercesJDK5Serializer.java看看XercesJDK5Serializer.java BaseXercesJDK5Serializer.java

You can set the property to use own Xerces to prevent the problem.您可以将属性设置为使用自己的 Xerces 来防止该问题。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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