Using JasperReports with a relative path

  • Paths must be absolute.
  • Only compile .jrxml files to .jasper files if the .jrxml is being modified. Usually you can just load the .jasper file and skip compilation altogether. It is much faster.
  • Store .jasper and .jrxml files outside of your web root.
  • Create the following parameters throughout all your reports:
       ROOT_DIR = "/full/path/to/reports/"
       IMAGE_DIR = $P{ROOT_DIR} + "images/"
       STYLES_DIR = $P{ROOT_DIR} + "styles/"
       SUBREPORT_DIR = $P{ROOT_DIR} + "subreports/"
       COMMON_DIR = $P{ROOT_DIR} + "common/"
  • Reference items relative to $P{ROOT_DIR} (e.g., $P{IMAGE_DIR} is defined in terms of $P{ROOT_DIR}).
  • Pass the value of $P{ROOT_DIR} in from your environment.
  • Loosely couple your application to any reporting framework you use.

Then use the expressions when necessary. For example, reference subreports as follows:

<subreportExpression>
  <![CDATA[$P{SUBREPORT_DIR} + "subreport.jasper"]]>
</subreportExpression>

This will allow the subreport directory to vary between environments.


I had the same problem, and I got the solution. First, put every object (subreport, image, etc) used in report and all extensions (.jasper, .jrxml) in one folder and put that folder in C:// disk. It is probably now placed somwhere in My Documents, or any path that has spaces between words, and then iReport sees it like "My%20Documents" and it confuses him.

So, put the folder in C://, put all stuff related to your report in the same folder and put relative paths to everything. This should work. Hope I helped anyone.