Call Jasper Report / Ireport From Java Application

Call Jasper Report / Ireport From Java Application Example describes about How to call Jasper Report and Ireport from a java application.

Jasper-Reports is a free reporting engine that could be written to screen, or to a printer or as HTML, PDF, RTF, Microsoft Excel, XML files, CSV format and ODT files.

Jasper-Report is using in Java related applications, including Web and Enterprise applications, to develop dynamic reports. It reads its instructions from jasper file (compiled) or XML dynamically

We can generate reports using two ways,

1) From "JRXML" (Source) file
2) From "Jasper" (Compiled) file
 

Required Libraries

You need to download

  1. JDK 6
  2. iReport-4.1.1 for designing the report

Following jar must be in classpath (Available from ireport installation directory)

  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

If you are created a JRXML/Jasper file using a particular version of Ireport (For example consider Ireport-4.7.0)

In order to run the report, you must use same set of jar's available on installed Ireport-4.7.0 directory (C:\Program Files\Jaspersoft\iReport-4.7.0\ireport\modules\ext)

If you are creating a report using Ireport-4.7.0 and compiling / running using Ireport-4.2.0 jar files, you will get java.lang.NullPointerException

1) Create PDF Report From JRXML File

JRXML file is a JasperReports Document. JRXML is the XML file format of JasperReport, which can be coded manually, generated, or created 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;

// Java Program To Call Jasper Report

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");
 
}
}

Output
Call Jasper Report

2) Create PDF Report From Jasper File

Note

Jasper file is a compiled format of JasperReports Document.

As it is pre-compiled generating reports from Jasper file will be very fast. It is recommended for the production environment due to its performance

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;

// How To Invoke Ireport From Java Application

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");

 
}
}
Output
Call Jasper Report










39 Responses to "Call Jasper Report / Ireport From Java Application"
  1. pradhumna 2011-10-09 21:06:06.0
  1. Coolashy1510 2011-10-09 21:26:06.0
  1. Sherin Matheew 2011-10-11 19:06:06.0
  1. admin 2011-10-11 21:16:16.0
  1. Swapnil Shirsat 2011-10-19 13:06:16.0
  1. admin 2011-10-21 21:06:46.0
  1. Swapnil Shirsat 2011-10-22 21:06:36.0
  1. DeD 2011-10-23 21:06:06.0
  1. admin 2011-10-24 21:06:06.0
  1. admin 2011-10-25 21:04:06.0
  1. Hardik Mishra 2011-10-25 21:06:26.0
  1. haris 2011-10-26 21:06:06.0
  1. admin 2011-10-27 19:06:06.0
  1. somphas 2011-10-28 21:06:06.0
  1. Felipe 2011-10-29 21:06:06.0
  1. dipesh bhavsar 2011-11-09 20:03:06.0
  1. chitra 2011-11-10 20:06:06.0
  1. admin 2011-11-11 20:06:02.0
  1. prabhu 2011-11-12 20:06:07.0
  1. admin 2011-11-13 15:06:04.0
  1. Prasad 2011-11-15 13:06:06.0
  1. admin 2011-11-16 14:06:03.0
  1. iman 2011-11-16 17:06:06.0
  1. admin 2011-11-16 20:06:09.0
  1. iman 2011-11-19 20:46:06.0
  1. Asim 2011-11-20 20:06:09.0
  1. admin 2011-11-21 20:06:09.0
  1. senn2007 2011-11-22 20:06:09.0
  1. admin 2011-11-23 14:06:09.0
  1. Sasindu 2011-11-25 20:06:09.0
  1. admin 2011-11-26 20:06:29.0
  1. Javier 2011-11-27 20:06:09.0
  1. admin 2011-11-28 20:06:09.0
  1. Javier 2011-11-28 22:06:09.0
  1. Javier 2011-11-29 00:06:09.0
  1. Mahendra 2011-11-29 20:06:09.0
  1. William Tekpeh 2011-11-29 23:06:02.0
  1. admin 2011-11-29 23:16:02.0
  1. Gajendra Solanki 2011-11-30 00:06:02.0

Your email address will not be published. Required fields are marked *