Jump to content
Changes to the Jaspersoft community edition download ×

Filling a report containing more subreports from Java


andrey87

Recommended Posts

Hello,
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.design.JasperDesign;
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) {
            e.printStackTrace();
        }
    }
}

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(RepositoryUtil.java:153)
    at net.sf.jasperreports.repo.RepositoryUtil.getReport(RepositoryUtil.java:112)
    at net.sf.jasperreports.engine.fill.JRFillSubreport.loadReport(JRFillSubreport.java:398)
    at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateReport(JRFillSubreport.java:365)
    at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateSubreport(JRFillSubreport.java:427)
    at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluate(JRFillSubreport.java:341)
    at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:381)
    at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:500)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2022)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:748)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:255)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:115)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:582)
    at net.sf.jasperreports.engine.fill.BaseReportFiller.fill(BaseReportFiller.java:414)
    at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:121)
    at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:667)
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:983)
    at it.jasperreports.test.RunApp.main(RunApp.java:30)

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,
Andrea
 

Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

  • 3 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...