JasperReportsIntegration – full path requirement and workaround

Well, some people have already been struggling with this problem and rather than replying individually, I can just as well take the time to post about that.

I would rather fix it, but unfortunately I don’t have the time to investigate the issue right now. Seems to me like bug or I don’t use the JasperReports libraries correctly ;). Not sure. But it seemed to have worked at times.

Ok, so what is the issue? We are talking about my JasperReportsIntegration toolkit, available at these locations:

When you reference an image or a subreport in the same directory as the report file (e.g. test.jasper) you would ideally use a relative path, i.e. no path at all if the image is located within the same directory.

It does work when you design your report in the iReport designer or in the JasperSoft studio, but it does not work when the report is deployed using the JasperReportsIntegration toolkit.

You will then receive an error like: java.lang.RuntimeException: Byte data not found at : opal_logo_50px_hoch.jpg

Snap 2014-04-15 at 00.10.57

But there is a workaround to that.

In my source code I already determine at runtime the correct path to the report file (e.g. report.jasper). And I do use that to set the internal parameter SUBREPORT_DIR:

Snap 2014-04-15 at 00.14.50

But somehow the value is not picked up by the report, I don’t know why.

Anyhow, as long as the problem exists, you can use a different approach. As you can see, I also store the path in the variable “BaseDir” at runtime. Thus, you can define a variable called BaseDir in your report:

Snap 2014-04-15 at 00.19.05

, might give it a default parameter pointing to your current development directory ( e.g.: “/Users/daust/Dropbox/Projekt/prj_JasperReportsIntegration/src/reports/demo/” or “S:\\Develop\\src\\prj_JasperReportsIntegration\\src\\reports\\demo”) . But this is just for convenience, this will not be used at runtime:

Snap 2014-04-15 at 00.24.42

The path for an image will use the following expression, if you want to reference an image like opal_logo_50px_hoch.jpg in the same directory as the report for example:

$P{BaseDir} + "/opal_logo_50px_hoch.jpg"

At runtime this will be resolved to the proper path.

You can find a working example when you download the software, there is a report called test2.jrxml in the directory reports/demo.

Snap 2014-04-15 at 00.29.00

You can also test it directly using the integrated test application:

Snap 2014-04-15 at 00.30.00

Here is the result:

Snap 2014-04-15 at 00.32.05

Hope that helps,
~Dietmar.

 

2 replies
  1. Marcin Szymański
    Marcin Szymański says:

    We’ve also struggled with similar or maybe even the same problem with image data not found using relative paths. The solution was to add the images directory to tomcat classpath in catalina.properties

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>