JasperReports Tutorial: What is, How to Install, Report Example

What is JasperReports for Java?

JasperReports is an open-source reporting tool for Java that is used for reporting of applications. This reporting tool helps in writing rich content onto the screen, to the printer, or into a variety of files like HTML, PDF, XLS, RTF, CSV, XML, ODT and TXT. JasperReports can also be used for Java-enabled applications like Java EE or web applications to generate dynamic content.

In this JasperReports step by step tutorial, you will learn:

Jasper Reports- Environment Setup

Now in this Jaspersoft Studio tutorial, let's learn the step by step process for JasperReports environment setup:

Step 1) Click on Download Now option to install Jasper Reports Community edition.

Step 2) Select Jaspersoft Studio CE

Step 3) Click on Download button open the download TIB_js-studiocomm_6.9.0_windows_x86_64.exe file.TIB_js-studiocomm_6.9.0_windows_x86_64.exe

Step 4) Following screen will appear, select the I agree button.

Step 5) In the Next Dialog box click on Next button

Step 6) You will see the progress in the next screen.

Step 7) In the next screen, click on the finish button.

Step 8) Following screen will appear in your window.

Step 9) Click on Get started option.

Step 10) Following screen will appear you can start working with Jasper Reports studio.

Why use Jasper Reports?

Here, are many reasons for using a Jasper report tool:

Lifecycle of JasperReports

Now in this JasperReports tutorial, we will learn about Lifecycle of JasperReports.

Following are General flow of Life cycle of Jasper Reports:

Lifecycle of JasperReports
Lifecycle of JasperReports

Designing the report:

This will be the first step where we create the JRXML file. It is the file is a simple XML document which defines the report layout. You can develop this file using any text editor, or report designer.

However, if you are using the report designer, we can visualize the layout of the report, and the real structure of the JRXML should be ignored.

Compiling the report:

In this life cycle stage, the JRXML which is built in the first stage is compiled, and a binary object called the Jasper file (file with jasper xtn) is generated.

This compilation is done to get better performance during execution. These generated Jasper files should be shipped along with your application to run the reports.

Executing the report:

In this stage, the data from the application is gathered and filled in the compiled report format. There is a class called net.sf.jasperreports.engine.JasperFillManager which offers functions to fill the data in the reports.

The output will be stored in Jasper print file (file with jprint xtn) which can be used to print or export the report.

Exporting the report to the desired format:

In this last stage, you need to export the Jasper print file which is created earlier into any format using the class JasperExportManager. As Jasper offers multiple formats of exports, we can use the same input to create various representations of the data.

Features of Jasper Reports

Now, we will cover the features of JasperReports in this JasperReports step by step tutorial.

Here, are Important Features of Jasper Reports tool:

Jasper vs. Pentaho

Now, we will learn about the difference between Jasper and Pentaho in this Jasper Reporting tutorial.

Here are major differences between Jasper and Pentaho:

Parameters Jasper Pentaho
Reports Jasper uses a report for designing the reports. It used the Pentaho Report Designer.
ETL support Jaspersoft uses Talend Open Studio version for ETL purpose. It uses Pentaho Data Integrator as a default ETL tool.
Dashboard Dashboard functionality is present only in the Enterprise editor of Jaspersoft. Pentaho offers wide ranges of features in their dashboards.
Mobile Business Intelligence Jaspersoft has better mobile BI too. Pentaho used need to rely on a third-party tool to enhance mobile BI experience.
Documentation Better marketing, sites, and documentation. Documentation support is poor.
Best feature Easier to research and find what you need. Offers Data mining and iPhone integration
Paid version The paid version offers features like ad hoc query web Ul, dashboard creation web Ul. Offers feature like LDAP integration, SSO, auditing, etc.

Creating a Report Template Example

Now in this JasperReports tutorial, let's learn how to create a Report template in JasperReports.

The report template file used in JasperReports is a simple XML file having jrxml extension. This file contains the tag as its root element. JasperReports framework can handle different types of data sources.

Report template is designed and created using the following two steps –

Step 1) Creating the JRXML file in Jaspersoft Studio.

  1. Go to file option.
  2. Select the New sub-menu
  3. Click on Jasper Report option.

Step 2) Selecting the desired template.

  1. Select your template, for example, cherry.
  2. Click on the "Next" button.

Step 3) Selecting a parent folder.

  1. Select MyReports folder.
  2. Enter the file name, for example, "Cherry Report."
  3. Click on the "Next" button.

Step 4) In Data Source window, click on "New" button to create new data adapter.

Step 5) Selecting a data adapter.

  1. Select data adapter from the list box. For example, "Collection of JavaBeans."
  2. Click on the "Next" button.

Step 6) Entering data adapter details and selecting factory class.

  1. Enter a name for your adapter.
  2. Select factory class by clicking on the "…" button.

Step 7) Entering prefix or pattern.

  1. Enter the name prefix or pattern.
  2. Click matching items.
  3. Click on the "OK" button.

Step 8) Defining factory class and adding the path to jar files.

  1. Enter the name of a static method in the factory class. In this Jasper reports example, this is createBeanCollection.
  2. If your JavaBeans definition has field descriptions, and you would like to use these as names in Jaspersoft Studio, check use field description checkbox.
  3. Add a path to your jar files.
  4. Click on "Finish" button.

Step 9) Click on the "Next" button.

Step 10) Congratulations! You have added all necessary report creation information successfully. Click on "Finish" button.

The report will be generated as shown in below screen.

The above file requires to compile into JasperReport Binary format, which is called the jasper file.

The Interface net.sf.jasperreports.engine.design.JRCompiler, which is part of the JasperReports library, plays an important role in the compilation of this report template file.

Implementation of this interface can be written in Java, JavaScript, Groovy, or any other scripting language.

You can compile a JRXML file in the following two methods:

Understanding the different report bands

Following are standard report format adopted by most of the commercial report generating tools.

Let's Learn each reporting bands in detail:

Title:

This band is shown at the starting of the report. It can be used as the first page by setting the attribute isTitleNewPage="true."

Page Header:

Page header reporting band shown at the beginning of each page. It excludes the first page if the title band is used and the last page if this summary band is used with setting isSummaryWithPageHeaderAndFooter="false.".

Column Header:

Column headers appear before the detail band on each page.

Detail:

Detail section iterated for each record in data source supplied. It also allowed having multiple detail band ( 1, 2, etc.)

Column Footer:

This section appears below the detail band on each page where detail band is present. The default setting is the end of the page, but this can be switch to under last detail band by setting the attribute isFloatColumnFooter=" true."

Page Footer:

This section will be displayed at the end of each page excluding summary band, title band, the and last non-summary band.

Last Page Footer:

This section appears on the previous page (if not a summary band is given) in place of normal page Footer.

Summary:

The summary section only appears at the end of the report on a new page. when isSummaryNewPage="true" is set and with page header and footer is set isSummaryWithPageHeaderAndFooter="true".

Group Header:

This section only appears when it is defined every time the group expression change before the detail band.

Group Footer:

This section only displays if a group is defined every time before the group expression change after the detail band.

Background:

It is displayed on all the pages as background to all other reporting bands.

No Data:

This band will appear when no data source was passed, or the data source is empty and whenNoDataType="NoDataSection" is set.

Challenges faced while creating JasperReports

Here, are a few challenges you will face while creating Jasper Reports:

Disadvantages of using Jasper Reports

Here, are drawbacks/cons of using Jasper Reports.

Summary:

 

YOU MIGHT LIKE: