如何在jar文件中读取jrxml文件? [英] How to read jrxml file inside the jar file?
问题描述
我有一堆iReport源代码(jrxml)位于工作区内,路径如下:
I have a bunch of iReport source code (jrxml) locate inside the workspace with the following path:
<project>/report/<jrxml_file>
当我将源代码打包到jar文件中时。我将拥有此文件结构目录:
When I pack my source code into jar file. I will have this file structure directory:
<jar_file>/report/<jrxml_file>
在我的源代码中,我有这些代码来验证jrxml文件是否存在:
In my source code, I have these code to validate the existence of the jrxml file:
File file = new File("report/jrxml_file");
if (!file.exists()) {
return false;
}
当我通过命令执行此jar文件时:
When I execute this jar file through the command:
java -jar MyJar.jar
我提到错误提到特定的jrxml_file不存在。
I hit an error mention that the particular jrxml_file doesn't exists.
我怀疑:
- 我只是想知道我是否允许读取位于MyJar.jar中的jrxml_file?
- 我是否需要将jrxml_file解压缩为物理我可以阅读目录吗?
谢谢@!
推荐答案
如果文件在给定路径的jar顶部,您可以使用:
If the file is on top of your jar at given path, you can use this:
InputStream is = this.getClass().getResourceAsStream("/report/jrxml_file");
请参阅Java类文档:
See Java Class docs:
- http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)
从那里,您可以将其读作任何其他InputStream,例如使用Apache IOUtils,如下所述:
From there, you can read it as any other InputStream, for example using Apache IOUtils, as explained here:
- Read/convert an InputStream to a String
这篇关于如何在jar文件中读取jrxml文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!