[英]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.