0

I have a bunch of iReport source code (jrxml) locate inside the workspace with the following path:

<project>/report/<jrxml_file>

When I pack my source code into jar file. 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:

File file = new File("report/jrxml_file");
if (!file.exists()) {
   return false;
}

When I execute this jar file through the command:

java -jar MyJar.jar

I hit an error mention that the particular jrxml_file doesn't exists.

My doubt:

  1. I am just curious to know whether am I allow to read the jrxml_file which is locate inside MyJar.jar?
  2. Do I need to extract the jrxml_file to a physical directory before I can read it?

THanks @!

huahsin68
  • 6,241
  • 19
  • 74
  • 111
  • 2
    Typically you would not put .jrxml files into a .jar. You can. There's nothing fundamentally wrong with it. But it's much more common to compile them to .jasper files and save these in your .jar file. – mdahlman Feb 24 '12 at 05:13
  • 1
    Is that mean I should compile into jasper files before including them into jar file? The way I design the program is to read the jrxml file and then compile it into jasper file. Do I need to separate out the compilation section into one program and then execute that program first then only jar the jasper file? Please advice. – huahsin68 Feb 25 '12 at 12:23
  • 2
    Yes. Normally there is no need to make the .jrxml files part of your application. So you compile them as part of your development steps, and you put the .jasper files into your application. – mdahlman Feb 26 '12 at 22:50
  • possible duplicate of [How to embed JasperReport sub-report into a jar](http://stackoverflow.com/questions/13322899/how-to-embed-jasperreport-sub-report-into-a-jar) – ericbn Oct 23 '14 at 01:32

1 Answers1

2

If the file is on top of your jar at given path, you can use this:

InputStream is = this.getClass().getResourceAsStream("/report/jrxml_file");

See Java Class docs:

From there, you can read it as any other InputStream, for example using Apache IOUtils, as explained here:

Community
  • 1
  • 1
icyrock.com
  • 25,648
  • 4
  • 58
  • 78