![](/img/trans.png)
[英]How apache.commons.lang3 is added to Jhipster external libraries without adding to pom.xml file?
[英]How to create XML file without external libraries in Java 9?
在Java 9中如何在没有外部库的情况下创建
XML
文件?
当前,我正在使用javax.xml.bind.JAXBContext
和javax.xml.bind.Marshaller
类来执行此操作,但是在Java 9上这些类不再存在。 因此,在使用Java 9时,我遇到一个异常: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBContext
。
我可以使用较低的Java版本创建XML
文件,但我需要它与Java 9兼容。
List<Athlete> athletes = new ArrayList<>();
Places places = new Places();
places.setAthletes(athletes);
JAXBContext context = JAXBContext.newInstance(Places.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(places, new File(outputPath);
marshaller.marshal(places, System.out);
List<Athlete> athletes = new ArrayList<>(); Places places = new Places(); places.setAthletes(athletes); JAXBContext context = JAXBContext.newInstance(Places.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(places, new File(outputPath); marshaller.marshal(places, System.out);
PS: 我已经搜索了该问题的答案,但未找到任何内容,因此它不应是重复的问题。
正如我提到的那样, 我只需要使用Java内部库即可。
您可以在Java 9中使用这些库,但是需要包含它们的模块。 确保您的module-info.java
对java.xml
模块有要求:
module myModule {
requires java.xml;
}
由于需要JAXB模块提供更多功能,因此requires
在module-info.java
文件中添加更多module-info.java
语句。 例如,完成module-info.java
,您的module-info.java
文件可能看起来更像以下内容:
module myModule {
java.xml
java.xml.bind
java.xml.ws
java.xml.ws.annotation
}
有关更多信息,请参见如何解决Java 9中的java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.