简体   繁体   English

生成jar时未打包.xml文件

[英].Xml file is not packaged when I build jar

This is the structure of my project in IntellijIDEA 这是我在IntellijIDEA中的项目结构

Structure 结构体

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文档,并返回一个新的DOM Document对象。

"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.getResourceClass.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.

相关问题 当我作为打包jar运行时,为什么文件路径没有占用? - Why File path is not taking when i run as packaged jar? Java 打包到 jar 文件时代理未运行 - Java Agent not running when packaged into jar file NoClassDefFoundError 我运行 Maven 打包 JAR 文件 - NoClassDefFoundError as I run a Maven packaged JAR file Java Netbeans,库在构建时未打包到jar文件中 - Java Netbeans, libraries not being packaged into jar file on build 将pom.xml打包为ejb时出现Maven错误,但打包为jar时消失 - Maven error when pom.xml is packaged an ejb, but goes away when packaged as jar Maven 使用打包的依赖项构建 jar - Maven build jar with packaged dependencies 当程序打包为jar时,无法读取文件并使用gson解析 - Fail to read file and parse with gson when the program packaged as jar Springboot app在应用打包为jar时检测不到应用属性文件 - Springboot app not detecting the application properties file when the application is packaged as jar 将数据导出到Excel可以在Eclipse中工作,但打包成.jar文件时则不能工作 - Exporting data to Excel works in eclipse but not when packaged as a .jar file 执行打包在jar中的WMV文件 - Executing wmv file packaged in jar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM