Reporting in Java using JasperReports and iReport tool



A report is a nicely formatted way of presenting the data that we have entered. Reports are all about querying a database and displaying the results in a nice format. There are many tools to generate reports. All of the tools have the following layout for a report page.




JasperReports is an engine that takes an XML file and forms a report out of that file using the data

source specified in the XML file. Writing this file by hand is not practical. So iReport tool is used to build reports.


iReport is a visual tool to obtain XML files for JasperReports. It provides a environment to design reports.


Installation and configuration
If we  have already installed on our machine a jdk (not simply a Java Runtime Enviroment, but a Java Development Kit) we are ready to start...

1. Unzip and copy the extracted directory where we want.
2. Look for a file called tools.jar in your jdk and copy it in the lib directory of iReport.

If you have Ant installed on our machine, modify the file iReport.bat or to adjust the ant installation path (default is c:\ant under Win32 and /ant under Linux).
3.Start iReport.bat or
If we don't have Ant installed (olny for win32 users), go to the directory no Ant and type:

At the first execution, iReport will create a directory (.ireport) in our home directory. Here will be stored all configuration files. iReport use XML files for configuration. At startup all jars in the iReport lib directory are added to the classpath. If we want to  use TTF Font files installed on our system, add to the classpath your fonts directory. iReport comes with an own fonts directory automatically added to the classpath. Started iReport...

1. go to menu->Tools->Options
2. go to tab external programs
3. set external viewers programs
2.3 Final test...

Ok, try if the configuration is ok....

Create a new blank report. Click on the iReport "Run without connection" button .After few seconds will appear our PDF opened with the program that we have set, as signal that all is OK.


Creating the XML File for JasperReports using iReport

Start by creating a new document in iReport. (File-New Document) In the report properties screen,

specify the report name as “EmployeeReport” and confirm other default values.

Before proceeding further, bear the following in mind:

Ø  Use Edit-Insert Element to insert different types of elements into the report. After selecting the element, you will see that the cursor turns into a “+” sign. Pressing the left button of the mouse and dragging it, specify the bounds of the inserted element. Double-clicking on the element   opens the property dialog, use this dialog to edit element properties. Property names are mostly self-explanatory.

Ø  Dynamic content of the report is provided by three things. 1. Parameters 2. Fields 3. Variables.All these three are added to the report as “Text Field”s through Edit-Insert element-Text field.

Ø  To specify a text field’s content to come from a Parameter, specify the Text field expression in  the property dialog of the element like the following: $P{parameterName}

Ø  Similarly, to specify a text field’s content to come from a field, specify the Text field      expression in the property dialog of the element like the following: $F{fieldName} fieldName   can only be columns in a table of the database. In our case, fieldName can be any of “EmployeeName”, “HoursWorked”, “Date”.

Ø  Similarly, to specify a text field’s content to come from a variable, specify the Text field expression in the property dialog of the element like the following: $V{variableName}

Ø  After specifying a text field’s content to come from either a Parameter, a Field or a Variable, you should define the corresponding content name in “Report Fields”, “Report Parameters” or  “Report Varibles” through “View-Report Fields”, “View-Report Parameters” or “View-Report Variables”.

Ø  By defining a “Report parameter”, we ensure that we will provide the JasperReport compiler

      with a Hashtable having an entry with key=”parameterName” and value=”parameterValue”                           prior to the compilation of the report.

Ø  Similarly, by defining a “Report field”, we ensure that resultset obtained by “Report Query”

      will contain a column named “fieldName”.

Ø  Variables are of two types: builtin variables or non-builtin variables. You will see examples of

      both in a few moments.





First, insert a static text as the title of report.


Specify title to be “Employee Work Hours Report”. Using geometric shapes, the appearance of the

report can be enhanced. However, for the time being, we won’t bother with the aesthetics.

To add a group to the report, press the “Groups” icon. On the opened dialog, press New. Specify

group name as “employee” and group expression as “$F{EmployeeName}”, press OK with other

fields in default values. You will see that two new bands are added as “employeeHeader” and


According to the instructions given above, obtain the design in following  Figure 4. Specify the Report Query through Edit-Report query as “SELECT * FROM tutorial_table ORDER BY EmployeeName”.

“Order by” is necessary because we created “employee” group with group expression “$F




 Finally save your design and compile it, you will have a jrxml file.


Creating the Final Report using JasperReports

After obtaining the jrxml file, it is only a few steps to obtaining the final report. The following code

segment in is all to get the final report viewed in a PDF viewer.



import org.jfree.chart.ChartFactory;

import org.jfree.chart.ChartFrame;




import org.jfree.chart.plot.PlotOrientation;




import dori.jasper.engine.JasperManager;

import dori.jasper.engine.JasperReport;

import dori.jasper.engine.JasperPrint;

import dori.jasper.engine.JasperPrintManager;

import dori.jasper.view.JasperViewer;

import java.util.HashMap;

import java.util.Map;

import java.sql.*;


public class Sample {

/** Creates a new instance of Sample */

public Sample() {



* @param args the command line arguments


public static void main(String[] args)




// First, load JasperDesign from XML and compile it into JasperReport

JasperDesign jasperDesign = JasperManager.loadXmlDesign("path-to-your-jrxmlfile\\


JasperReport jasperReport = JasperManager.compileReport(jasperDesign);

// Second, create a map of parameters to pass to the report.

Map parameters = new HashMap();

parameters.put("employeeChart", createEmployeeChartImage());

// Third, get a database connection

Connection conn = Database.getConnection();

// Fourth, create JasperPrint using fillReport() method

JasperPrint jasperPrint = JasperManager.fillReport(jasperReport, parameters, conn);

// You can use JasperPrint to create PDF

JasperManager.printReportToPdfFile(jasperPrint, "desired-path\\SampleReport.pdf");

// Or to view report in the JasperViewer


//Or create HTML Report

JasperExportManager.exportReportToHtmlFile(jasperPrint, "desired-path\\SampleReport.html);



catch(Exception e)







Above program converts XML to the final report by JasperReports.

One advantage of JasperReports is that it can export the final print to an HTML file which makes

dynamic reporting possible in web pages. The last line in Figure 5 exports the report to an HTML


Enter your email address:

Delivered by FeedBurner

Sandeep Joshi
Mathematics, Technology and Programming are my passion. I am a part of Java Ecosystem and through this blog, I contribute to it. I am here to blog about my interests, views and experiences.
I am on Google+ and Facebook.
I feel proud to be listed as a "National Memory Record Holder" in the Limca Book of Records, 2009 and have attempted for an International Memory record in the Guiness Book of Records. I can remember the value of PI upto 10,000 digits after the decimal (3.1415.....). You can contact me on ; I would like to hear from you :)