13

I have a web app and the client has requested to see some reports. The approach has been to use iReport and show them the report on screen.

I have already asked something like this. But today I have discovered that the paths to the report files (jrxml) are absolute. So I have to change the program so it accepts relative paths. I have been trying to do this, but it seems that neither the jrxml or the compiled (.jasper) files accepts relative paths to neither compile or to fill the report.

This is what I have got this far:

//path is generated as request.getContextPath() + "/jrxmlFiles/"
public void generateReport(HttpServletResponse res, ConexionAdmin con, String path) throws Exception{ 

    ServletOutputStream out = null;
    ByteArrayOutputStream bos    = new ByteArrayOutputStream();

    JasperDesign jasperDesign = JRXmlLoader.load(path);
    JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

  byte[] bytes = JasperRunManager.runReportToPdf(jasperReport, pars, con.initConexion());
        res.setContentType("application/pdf");
  res.setContentLength(bytes.length);
  out = res.getOutputStream();
  out.write(bytes, 0, bytes.length);

  res.setHeader("Cache-Control", "cache");
  res.setHeader("Content-Disposition", "attachment; filename=report.pdf"); 
  res.setHeader("Pragma", "cache");
  res.setContentLength(bos.size());

  out.write(bos.toByteArray());
  out.flush();
  bos.close();
  out.close(); 
  res.flushBuffer();
}

This seems to work with absolute paths, but throws me:

Exception Message
net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException

when changed to a relative path. I have searched the net with no success in how to change to my fits.

I have the javaDoc for the jasper API, but I rather not read it through if I can help it.

1
  • Seems I have forgoten to add the declaration of pars, it's just a new HasMap() (as the report I am trying has no variables). Sorry for the misplace!
    – Random
    Commented Mar 21, 2011 at 11:10

2 Answers 2

21
  • 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.

1
  • 1
    @Random: I am not suggesting that you don't use JasperReports. Far from it. I am suggesting that you use an implementation that uses JasperReports without being tightly coupled. (Effectively isolating architectural components.) Commented Apr 4, 2011 at 19:09
3

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.

3
  • This project was from quite a while, and I don't have the code anymore to test it. But the idea was that Jasper would be able to use some relative path such as /JRXML_files/account/account_report.jrxml (inside WebContent). Although in the time this has been posted Jasper has a new version and hopefully they accept this paths. Thanks and I hope I can try this soon.
    – Random
    Commented Mar 17, 2014 at 14:20
  • 1
    Yes, I understand what was your problem. And I had the same issue few days ago with iReport 3.7.2, and finally got what confused him. Whatsoever, just wanted to post answer somewhere, since I saw a lot of questions with the same issue.
    – Merjem
    Commented Mar 18, 2014 at 14:59
  • It is not usefull in a web application, like I see your solution works only in standalone apps.
    – J. Abel
    Commented Nov 20, 2019 at 1:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.