[英].Xml file is not packaged when I build jar
This is the structure of my project in IntellijIDEA 这是我在IntellijIDEA中的项目结构
When I build my jar file it doesn't package the characters.xml file. 当我构建jar文件时,它不会打包character.xml文件。
Document doc = builder.parse("characters.xml");
This is how I source it in the code. 这就是我在代码中来源的方式。 There is obviously a mistake here but I can't figure out where to put it and what path I should write. 显然这里有一个错误,但是我不知道该放在哪里以及应该写什么路径。
The documentation for DocumentBuilder.parse(String) states: DocumentBuilder.parse(String)的文档指出:
Parse the content of the given URI as an XML document and return a new DOM
Document
object. 将给定URI的内容解析为XML文档,并返回一个新的DOMDocument
对象。
"characters.xml"
is technically a valid URI, but it is a relative URI, and there is no base URI against which to resolve it. "characters.xml"
技术上讲, "characters.xml"
是有效的URI,但它是相对URI,并且没有可用于解决它的基础URI。
To read a file inside a .jar, you use Class.getResource or Class.getResourceAsStream . 要读取.jar中的文件,请使用Class.getResource或Class.getResourceAsStream 。 An example would be: 一个例子是:
Document doc;
try (InputStream stream = Controller.class.getResourceAsStream("characters.xml")) {
doc = builder.parse(stream);
}
Alternatively, you could convert the resource to a URI: 或者,您可以将资源转换为URI:
Document doc = builder.parse(
Controller.class.getResource("characters.xml").toString());
Note that both Class.getResource and Class.getResourceAsStream return null if the argument is not a path of an existing jar entry. 请注意,如果参数不是现有jar条目的路径,则Class.getResource和Class.getResourceAsStream均返回null。 You will need to move characters.xml into your src\\sample directory. 您将需要将character.xml移动到src \\ sample目录中。 You may want to verify that the .jar file contains characters.xml by examining it in your IDE's “Files” area. 您可能需要通过在IDE的“文件”区域中检查.jar文件来验证它是否包含character.xml。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.