Filling a report containing more subreports from Java

I've designed 3 simple reports (one main report, and two subreports) with Jaspersoft Studio feature into Eclipse. Each report contains only one static text field (they have not any connection to a database).
The main report contains a static text field and contains the others two sub reports. Very simple situation.

This is my code:

package it.jasperreports.test;

import java.util.HashMap;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.xml.JRXmlLoader;

public class RunApp {

    // Path of main .jrxml report
    private final static String JRXML = "C:/ws/Test_JasperReports/src/main/java/it/jasperreports/test/report.jrxml";
    // Paths of .jrxml subreports
    private final static String JRXML_SUB1 = "C:/ws/Test_JasperReports/src/main/java/it/jasperreports/test/subreport1.jrxml";
    private final static String JRXML_SUB2 = "C:/ws/Test_JasperReports/src/main/java/it/jasperreports/test/subreport2.jrxml";
    // Path of PDF output
    private final static String PDF = "C:/ws/Test_JasperReports/src/main/java/it/jasperreports/test/report.pdf";

    public static void main(String[] args) {
        try {
            JasperDesign design = JRXmlLoader.load(JRXML); // Load in memory of the main report design
            JasperReport report = JasperCompileManager.compileReport(design); // Compiling the main report design into a report
            JasperPrint print = JasperFillManager.fillReport(report, new HashMap<>(), new JREmptyDataSource()); // Filling the report
            JasperExportManager.exportReportToPdfFile(print, PDF); // Exporting the report into PDF
        } catch (JRException e) {

When I run the code, I've this error (when the instruction JasperPrint print = JasperFillManager.fillReport is executed):

net.sf.jasperreports.engine.JRException: Resource not found at: src/main/java/it/jasperreports/test/subreport1.jasper.
    at net.sf.jasperreports.repo.RepositoryUtil.getResourceFromLocation(
    at net.sf.jasperreports.repo.RepositoryUtil.getReport(
    at net.sf.jasperreports.engine.fill.JRFillSubreport.loadReport(
    at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateReport(
    at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateSubreport(
    at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluate(
    at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(
    at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(
    at net.sf.jasperreports.engine.fill.BaseReportFiller.fill(
    at net.sf.jasperreports.engine.fill.JRFiller.fill(
    at net.sf.jasperreports.engine.JasperFillManager.fill(
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(
    at it.jasperreports.test.RunApp.main(

I think I've this error because the .jasper files of two subreports not exist; in fact if I try to compile the two subreports with Jaspersoft Studio, the .jasper are created and I have not the error.

It could be possible to do the compilation of subreports at run-time?
And, in some cases I could have only one subreport, in other cases more report: it could be possible to retrieve at run-time the list of subreports and compile them?

Is it mandatory to have the physical subreports .jasper files or is it possible to compile them in memory and pass them to main report?

Any idea?

Thanks and regards,

andrey87's picture
Joined: Jan 28 2017 - 12:52am
Last seen: 11 months 7 hours ago
have you fixed it?i use subrport in servlet,and got this error too.
broderick_wang - 5 years 9 months ago

1 Answer:

You may want to check what the Subreport's Expression is referening - the .jasper or the .jrxml.  If they reference the jrxml, I believe the report will be compiled at run time. 

sandersen's picture
Joined: Feb 19 2014 - 10:16am
Last seen: 4 years 2 months ago