简体   繁体   English

如何读取jar文件中的jrxml文件?

[英]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: 我的疑问:

  1. I am just curious to know whether am I allow to read the jrxml_file which is locate inside MyJar.jar? 我只是想知道我是否允许读取MyJar.jar中的jrxml_file?
  2. Do I need to extract the jrxml_file to a physical directory before I can read it? 在读取jrxml_file之前,是否需要将其提取到物理目录中?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM