how we add sub report in java code

Hi all,

I am new in jasper design, now I am trying to add a sub report (.jrxml or .jsper) in our master report (JasperDesign Code) in java class but I am not getting any hint that how I can do it   .


Can any on please provide me a sample code for my problem.

Please do it ASAP, Thank’s in Advance


Post Edited by test4only at 10/28/2009 11:49
test4only's picture
Joined: Jul 16 2009 - 2:52am
Last seen: 13 years 10 months ago

7 Answers:

Look in the subreport example from jasper reports download. In the .java file you'll see an entry like this:

JasperReport subreport = (JasperReport)JRLoader.loadObjectFromLocation("ProductReport.jasper");

//Preparing parameters
Map parameters = new HashMap();
parameters.put("ProductsSubreport", subreport);

Then in you .jrxml file there will be a section like this. iReport will create this. In iReport the parameter class is net.sf.jasperreports.engine.JasperReport. The subReportParameter Name is the parameter where the data link is passed.  This part is from my report since it include the parameter name for the data link between the two. (that and I happened to have it opened :).

<band height="27">
<reportElement x="0" y="0" width="572" height="25"/>
<subreportParameter name="OPPTSOL_LINK">
<subreportExpression class="java.lang.String"><![CDATA["SolutionOverview_Solution1.jasper"]]></subreportExpression>

Post Edited by jvway at 11/02/2009 23:32
jvway's picture
Joined: Mar 9 2009 - 11:43am
Last seen: 7 years 3 months ago
Thanks for replay,
But this code we can use in our Master.jrxml while designing with ireport or jrxml designing tool.

In fact I want to write this code through java code because master report I am designing by the use of JasperDesign API.

So further I am requesting to Please Provide the Java Code for Adding The sub report (jasper Or jrxml) file .


Sarvesh Awasthi

Send mail me :

test4only's picture
Joined: Jul 16 2009 - 2:52am
Last seen: 13 years 10 months ago


Did you find a solution to your problem?

I am facing the same issue.


hedges's picture
Joined: Oct 26 2010 - 2:52am
Last seen: 12 years 7 months ago

Are you trying to write a Java scriptlet which will add a subreport into your report?

hozawa's picture
Joined: Apr 24 2010 - 4:31pm
Last seen: 3 years 11 months ago

i am also facing the same problem . getting the given error.

net.sf.jasperreports.engine.JRException: Could not load object from location : .\\netIncomeStatement_subreport0.jasper

when i am execute this report as standalone it works fine. but when i calling from my java code it show the above error.
please help me. I am in trouble.


asmithndreww's picture
Joined: Jul 29 2011 - 8:01am
Last seen: 11 years 10 months ago


Perhaps it's a classpath matter here. When you are running the report from java, the relative path "./" does not point to the same current directory as when running the report from iReport.

Try to see which is the current directory when running from java, and set the corect path to the netIncomeStatement_subreport0.jasper object.

Hope this helps,


shertage's picture
Joined: Sep 26 2006 - 8:06pm
Last seen: 11 months 2 weeks ago

I think i had an answer for this. But it is some sort temp solution.

Basically the fillManager is looking for the compiled object while jrxml is not compiled.

So in the SUBREPORT_DIR should look like:

class="net.sf.jasperreports.engine.JasperReport" isforprompting="false" name="SUBREPORT_DIR"

and the subreportExpression in the subreport tag under main report should be the above SUBREPORT_DIR

for some reason i can not paste my code here, it looks like the HTML editor is messed up here. I am using IE10




davidcurious's picture
Joined: Jan 12 2017 - 4:05pm
Last seen: 6 years 1 week ago