Using Jasperreports with a Relative Path

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.

Jasper Reports Change absolute path to relative path?

Use java.io.File to get the absolute path from a relative path. es.

File f = new File("yourRelativePath/ConsumptionReport.jrxml");
JasperReport jr = JasperCompileManager.compileReport(f.getAbsolutePath());

Seeing the problem to find the relative path of your deployed web application I suggest you check out these questions.

Need to find the web application path

What does servletcontext.getRealPath("/") mean and when should I use it

Relative path for the Subreport

Well I got an answer.

You can't. You can't use relative paths for your subreports. You may try to compile the .jrxml files at runtime or something, but relative paths don't seem to work for the projects with external servers or file systems.

How to set relative classpath for jasper reports

If you are working with Jasper Swing application then it's best practice that path must be absolute. Just compile all .jrxml files to .jasper files and load compiled .jasper file always. It is fast.

create folder like jaspers, images, styles out side of application. And create system/environment variable like below.

JASPERDIR = "c:\\app\\jaspers";
STYLES = "c:\\app\\styles";
IMAGES="c:\\app\images";

Now in your swing application Use the System.getenv() method, passing the name of the variable to read the path.

In this way your swing application is flexible enough every time. You can change images, styles, .jasper files any time without restarting your swing application.

How to set relative path of images in report?

I've personally had not tried this way with a jar, but i hope it helps. As you've stated the problem comes from the file path. On the iReport tool you can use relative paths and it works on the preview, but when the report generation is integrated within an application, it can only work with absolute paths.

The way i've dealt with this drawback was by getting the absolute path of the image inside the java application, and passing it as a parameter to the report. Example:

 String image = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/Cards_Template/front.jpg");

NOTE: I've built a JSF app, that's why i'm getting the path from it's context. If you don't, Java's IO or NIO API does have some methods to do the same. Basically, i get an absolute path from a relative path.



Related Topics



Leave a reply



Submit