问题
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>
回答1:
It's probably easiest to remove SUBREPORT_DIR
completely. iReport adds it by default as a convenience to keep track of your subreports. But you don't need to keep it.
In your case the reports are in the same directory and in the .jar, so they'll be on the classpath. Change the reference in the .jrxml from $P{SUBREPORT_DIR} + "mysubreport.jasper"
to simply "mysubreport.jasper"
. Then delete the parameter from the report and get rid of your code that tries to figure out what directory to use.
回答2:
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>
回答3:
If I am not mistaken you should be able to reference the subreport just based on the location of the compiled jasper file inside the jar. For instance if the subreport is in a package named test.reports
for the SUBREPORT_DIR
value you should be able to pass in /test/reports/
.
If that does not work, try moving the compiled reports into the default package and just set SUBREPORT_DIR
as /
instead.
回答4:
In the "KEY" property of the subreport object within the main file, try assigning the subreport name defined in the subreport file, so the subreport value will be returned to the main report.
来源:https://stackoverflow.com/questions/10739547/passing-subreport-dir-to-subreport-when-the-path-points-inside-a-jar-file-jaspe