Call Jasper Report / Ireport From Java Application

JasperReports is an open source Java reporting tool that can write to screen, to a printer or into PDF, HTML, Microsoft Excel, RTF, ODT, Comma-separated values and XML files.

It can be used in Java-enabled applications, including Java EE or Web applications, to generate dynamic content. It reads its instructions from an XML or .jasper file dynamically.

We can generate reports using two ways,

1) From “JRXML” (Source) file
2) From “Jasper” (Compiled) file

Following jar must be in classpath

  1. commons-beanutils-1.8.2.jar
  2. commons-collections-3.2.1.jar
  3. commons-digester-1.7.jar
  4. commons-logging-1.1.jar
  5. groovy-all-1.7.5.jar
  6. iText-2.1.7.jar
  7. jasperreports-4.1.1.jar

Note : iReport for designing the report.

Create PDF Report From JRXML File

JRXML file is a JasperReports Document. JasperReports are defined in an XML file format, called JRXML, which can be hand-coded, generated, or designed using a tools like IReport, JasperAssistant etc

Execution of report from JRXML file will be very slow, as it need to compile before the execution


import java.io.IOException;
import java.util.HashMap;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

public class PdfFromXmlFile {
public static void main(String[] args) throws JRException, IOException {

JasperReport jasperReport = JasperCompileManager.compileReport("report.jrxml");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,new HashMap(), new JREmptyDataSource());
JasperExportManager.exportReportToPdfFile(jasperPrint, "sample.pdf");
}
}

Create PDF Report From Jasper File

Jasper file is a compiled format of JasperReports Document.

Execution of report from Jasper file will be very fast, as it is pre-compiled

It is recommended for the production environment

import java.io.IOException;
import java.util.HashMap;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

public class PdfFromJasperFile {
public static void main(String[] args) throws JRException, IOException {

JasperPrint jasperPrint = JasperFillManager.fillReport("report.jasper",  new HashMap<String, Object>(),
new JREmptyDataSource());
JasperExportManager.exportReportToPdfFile(jasperPrint, "sample.pdf");

}
}

Hope you will like this. Cheers… 🙂

Advertisements

About Sanju
I am Software Programmer. I am working in JAVA/J2EE Technologies.

One Response to Call Jasper Report / Ireport From Java Application

  1. Siva Krishna says:

    Hello Sanju,
    Good Explanation,I am working on Jasper Reports ,I need plain reports to excel, with no data formatting, no page break, etc Using I Report Tool .Is it possible in I report .Could you please give me the Explanation with example.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: