Jasper Reports - Example

Posted on June 11, 2009

Jasper Reports is one of the best 'Business Intelligence (BI)' tools/frameworks, which can be used for generating reports for Java/Groovy applications. It is just partly 'Open Source', but if you can practice or have good enough work experience in Java, you can easily build basic/complex reports.

'Jasper Reports' framework can handle different kinds of data sources, but in this post I will show how to generate a basic report, just by passing a collection of Java data objects (Java beans), to the Jasper Report Engine. Below is the code for the data object (Java bean):

package com.chetty.reporting.beans;

public class DataBean {
private String name;
private String occupation;
private String place;
private String country;

public void setName(String name) {
this.name = name;

public String getName() {
return name;

public void setOccupation(String occupation) {
this.occupation = occupation;

public String getOccupation() {
return occupation;

public void setPlace(String place) {
this.place = place;

public String getPlace() {
return place;

public void setCountry(String country) {
this.country = country;

public String getCountry() {
return country;

And below is the code for the class file with business logic to generate a collection of java bean objects, which is further passed to the Jasper report engine, to generate the report:

package com.chetty.reporting.business;

import java.util.ArrayList;

import com.chetty.reporting.beans.DataBean;

public class DataBeanMaker {
public ArrayList getDataBeanList() {
ArrayList dataBeanList = new ArrayList();

//dataBeanList.add(produce("Babji, Chetty", "Engineer", "Nuremberg", "Germany"));
dataBeanList.add(produce("Albert Einstein", "Engineer", "Ulm", "Germany"));
dataBeanList.add(produce("Alfred Hitchcock", "Movie Director", "London", "UK"));
dataBeanList.add(produce("Wernher Von Braun", "Rocket Scientist", "Wyrzysk", "Poland (Previously Germany)"));
dataBeanList.add(produce("Sigmund Freud", "Neurologist", "Pribor", "Czech Republic (Previously Austria)"));
dataBeanList.add(produce("Mahatma Gandhi", "Lawyer", "Gujarat", "India"));
dataBeanList.add(produce("Sachin Tendulkar", "Cricket Player", "Mumbai", "India"));
dataBeanList.add(produce("Michael Schumacher", "F1 Racer", "Cologne", "Germany"));

return dataBeanList;

private DataBean produce(String name, String occupation, String place, String country) {
DataBean dataBean = new DataBean();


return dataBean;

And below is the main class file, which gets the java bean collection from the class (with business logic) and passes it to the Jasper report engine, to generate the report:

package com.chetty.reporting.engine;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.chetty.reporting.beans.DataBean;
import com.chetty.reporting.business.DataBeanMaker;
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.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;

public class Reporter {
public static void main(String[] args) throws Exception {
InputStream inputStream = new FileInputStream ("reports/test_jasper.jrxml");

DataBeanMaker dataBeanMaker = new DataBeanMaker();
ArrayList dataBeanList = dataBeanMaker.getDataBeanList();

JRBeanCollectionDataSource beanColDataSource = new

Map parameters = new HashMap();

JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, beanColDataSource);
JasperExportManager.exportReportToPdfFile(jasperPrint, "c:/reports/test_jasper.pdf");

To design the jasper report design file, you can either use iReport or the JasperAssistant Plugin for Eclipse. I used iReport, as it has more features and options. You can find the jasper report design file here: test_jasper.jrxml

When you run the 'Reporter' class, the following pdf file is generated:

Test Jasper

You can find all the source files here: jaspertest

Blog Categories
The views expressed on this blog are my personal views and do not reflect the views of my employer or campaigns I am supporting.

All sample code is provided for illustrative purposes only. These examples have not been thoroughly tested under all conditions. The writer therefore, cannot guarantee or imply reliability, serviceability, or function of these programs.

All programs contained herein are provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.