[英]How to read jrxml file inside the jar file?
I have a bunch of iReport source code (jrxml) locate inside the workspace with the following path: 我有一堆iReport源代码(jrxml)通过以下路径位于工作区中:
<project>/report/<jrxml_file>
When I pack my source code into jar file. 当我将源代码打包到jar文件中时。 I will have this file structure directory:
我将拥有以下文件结构目录:
<jar_file>/report/<jrxml_file>
In my source code, I have these code to validate the existence of the jrxml file: 在我的源代码中,我具有以下代码来验证jrxml文件的存在:
File file = new File("report/jrxml_file");
if (!file.exists()) {
return false;
}
When I execute this jar file through the command: 当我通过命令执行此jar文件时:
java -jar MyJar.jar
I hit an error mention that the particular jrxml_file doesn't exists. 我碰到一个错误提示,说特定的jrxml_file不存在。
My doubt: 我的疑问:
THanks @! 谢谢 @!
If the file is on top of your jar at given path, you can use this: 如果文件位于给定路径的jar顶部,则可以使用以下命令:
InputStream is = this.getClass().getResourceAsStream("/report/jrxml_file");
See Java Class docs: 请参阅Java类文档:
From there, you can read it as any other InputStream, for example using Apache IOUtils, as explained here: 从那里,您可以将其作为任何其他InputStream读取,例如使用Apache IOUtils,如下所示:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.