Search code examples
javajasper-reportsireport

Passing SUBREPORT_DIR to subreport when the path points inside a JAR file [Jasper Reports 4.5]


I'm having problems passing the SUBREPORT_DIR path to my subreport. The reports are actually in the same folder, but inside a JAR file. I've tried something like this (might be inaccurate):

parameterList = new HashMap<String, Object>();

URL mainReport = this.getClass().getResource("mainReport.jasper");
String mainReportPath = mainReport.getPath();
String subreportDir = mainReportPath.substring(0, mainReportPath.lastIndexOf("/")+1);

parameterList.put("SUBREPORT_DIR", subreportDir);

This path points inside the JAR file, but I get net.sf.jasperreports.engine.JRException: Resource not found at : ... upon report generation. How can I make this work?

EDIT:

I tried to define my subreport without the SUBREPORT_DIR, but no luck:

<subreport>
      <reportElement x="0" y="32" width="475" height="17"/>
      <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{current})]]></dataSourceExpression>
      <subreportExpression><![CDATA["mysubreport.jrxml"]]></subreportExpression>
</subreport>

Solution

  • My main problem was, that I was using a custom JAR loader, which Jasper didn't know of. The good news is it is also possible to pass a subreport as java.net.URL or java.io.InputStream:

    Java code:

    InputStream subInputStream = this.getClass().getResourceAsStream("sub.jasper");
    parameterList.put("SUBREPORT_INPUT_STREAM", subInputStream);
    

    JRXML:

    <subreportExpression class="java.io.InputStream"><![CDATA[$P{SUBREPORT_INPUT_STREAM}]]></subreportExpression>