About Manu PK

Manu develops software applications using Java and related technologies. Geek, Tech Blogger, open source and web enthusiast.

Using Jasper Reports to create reports in Java

Last week I was trying to create a report using Jasper. In this post I will document some of the resources and links so that it will be useful for any one looking for similar information.

I will cover life cycle of Jasper reports, examples and Dynamic Jasper.

The Jasper Reports is the world’s most popular open source reporting engine. It is entirely written in Java and it is able to use data coming from any kind of data source and produce pixel-perfect documents that can be viewed, printed or exported in a variety of document formats including HTML, PDF, Excel, OpenOffice and Word.

JasperReport Life Cycle

As in the image the life cycle has 3 distinct phases,

1. Designing the Report

In this step involves creation of the JRXML file, which is an XML document that contains the definition of the report layout. We can use the either iReport Designer or a text editor to manually create it. Using iReport Designer, the layout is completely designed in a visual way, so you can ignore the real structure of the JRXML file.

Here is the detailed tutorial on designing a report using iReport. We can also use Dynamic Jasper described later in the article to design a report.

2. Executing the report.

Before executing a report, the JRXML must be compiled in a binary object called a Jasper file(*.jasper). This compilation is done for performance reasons. Jasper files are what you need to ship with your application in order to run the reports. Once the report is compiled it is filled with data from the application. The class net.sf.jasperreports.engine.JasperFillManager provides necessary functions to fill the data in the reports.

The report execution is performed by passing a Jasper file and a data source to JasperReports. There are plenty of types of data sources, it’s possible to fill a Jasper file from an SQL query, an XML file, a csv file, an HQL (Hibernate Query Language) query, a collection of Java Beans, etc… If you don’t find a suitable data source, JasperReports is very flexible and allows you to write your own custom data source.

JasperFillManager.fillReportToFile( ‘MasterReport.jasper’ , parameters, getDataSource());

This operation creates a Jasper print file (*.jrprint), which used to either print or export the report.

3. Exporting to the desired format

Using the Jasper print file created in the previous step we shall be able to export it into any format using JasperExportManager. Jasper provides various forms of exports. This means with the same input we can create multiple representation of the data. Jasper inernally uses different APIs to create documents. But these complexity are hidden by the simpler
JasperExportManager.

JasperExportManager. exportReportToPdfFile( ‘MasterReport.jrprint’ );

In a nutshell the life cycle can be summarized in the below image

Image from Ramki Tech

References and other good articles on Jasper Reports Life Cycle

  1.  Jasper Library Wiki
  2.  Jasper Reports Wiki
  3.  Jasper Reports in Ramki Java Blog
  4.  JasperReport – Open Source Java Reporting Framework

 
Examples

I have found it really hard to find a working example of Jasper report. But it is right there inside the package shipment!. Once you have downloaded the Jasper Library go to demo\samples, you will find a lot of sample programs. Many of these needs a working HSQL DB connection, to activate it go to demo\hsqldb and start the server. Every folder has a readme.txt file which will help you in understanding how to run it. All the examples can be executed using ant tasks.

Here is a list of few other sources.

  1. Samples from the Jasper Library
  2. Java Reporting With Jasper Reports – Part 2
  3. Jasper Reports – Example
  4. Spring MVC 3.1 and JasperReports

 
Simplify report creation using Dynamic Jasper

DynamicJasper (DJ) is an open source free library that hides the complexity of Jasper Reports, it helps developers to save time when designing simple/medium complexity reports generating the layout of the report elements automatically.

The project homepage provides lots of examples and code snippets on how to use the library. I have been using it for some time and it is a pretty stable replacement for the JRXML file.While using dynamic jasper the report design is coded in Java. Which means every time the report is compiled, filled and exported. By using dynamic jasper we are replacing the first step in the above mentioned jasper life cycle. Even with dynamic jasper you need the jasper library and other dependent files.

Here is some more examples of Dynamic Jasper usage.

  1. HOW TO page on Dynamic Jasper
  2. Spring 3 – DynamicJasper – Hibernate Tutorial: Concatenating a DynamicReport
  3. Spring 3 – DynamicJasper – Hibernate Tutorial: Using Plain List

 
Reference: Using Jasper Reports to create reports in Java from our JCG partner Manu PK at the The Object Oriented Life blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

4 Responses to "Using Jasper Reports to create reports in Java"

  1. Sanjeet says:

    main budhwa hun !

  2. Good job..Nice to posting example links.

  3. Dmitri says:

    Finally a simple article containing everything I have been looking for, very well covered. Thank you!

  4. hamed says:

    thank you

Leave a Reply


× 1 = seven



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close