title: Using JasperReports with a relative path
body: <p>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.</p>
  
  <p><a href="https://stackoverflow.com/questions/2071075/using-printwriter-and-outputstream">I have already asked something like this</a>. 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. </p>
  
  <p>This is what I have got this far:</p>
  
  <pre><code>//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();
  }
  </code></pre>
  
  <p>This seems to work with absolute paths, but throws me:</p>
  
  <pre><code>Exception Message
  net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException
  </code></pre>
  
  <p>when changed to a relative path. I have searched the net with no success in how to change to my fits. </p>
  
  <p>I have the javaDoc for the jasper API, but I rather not read it through if I can help it. </p>