JasperReports Tutorial: What is Jasper report for Java? 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.

How to Install Jasper Reports- Environment Setup

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

Step 1) Download jasper reports community edition
Go to this Link and click on Download Now option to install Jasper Reports Community edition.

Install Jasper Reports- Environment Setup

Step 2) Select Jaspersoft Studio CE
From the community editions, Select Jaspersoft Studio CE

Install Jasper Reports- Environment Setup

Step 3) Download Jaspersoft Studio
Go to this link and 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.

Install Jasper Reports- Environment Setup

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

Install Jasper Reports- Environment Setup

Step 5) Choose location
Select your installation location and click on Next button.

Install Jasper Reports- Environment Setup

Step 6) Progress is running now
You will see the progress on the next screen.

Install Jasper Reports- Environment Setup

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

Install Jasper Reports- Environment Setup

Step 8) Progress is complete
Following screen will appear in your window.

Install Jasper Reports- Environment Setup

Step 9) Get Started
Next, Click on Get started option.

Install Jasper Reports- Environment Setup

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

Install Jasper Reports- Environment Setup

Why use Jasper Reports?

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

  • It offers report generation performance and delivery to users.
  • It helps you to represent your data in textual or graphical format.
  • It is a standalone and embedded reporting server for reporting compilation and execution.
  • It allows role-based access control to all reports in the repository.
  • A spring security framework for integration with in-house authentication systems
  • It offers secure, schedule, and automatic delivery interactive reporting engine.
  • You can embed reports into Java or non-Java Application.
  • Various formats of reports can be exported.
  • Developers can supply data in multiple ways.
  • It can generate watermarks that are laid over the primary image.
  • It offers a complete set of SOAP, REST, and Web Services.
  • It allows you to use single report data from multiple data sources.

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:

  • Offers flexible report layout.
  • Developers can supply data in multiple ways.
  • It allows you to accept data from multiple data sources.
  • Watermarks can also be applied.
  • Multiple data sources can be used to transfer data.
  • You can generate subreports
  • It can generate subreports.
  • It helps you to export reports in a variety of formats like PDF, HTML, RTF, Microsoft Excel files.
  • The engine allows report definitions to include charts.

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 purposes. 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.

Creating a Report Template Example

Step 2) Selecting the desired template.

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

Creating a Report Template Example

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.

Creating a Report Template Example

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

Creating a Report Template Example

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.

Creating a Report Template Example

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.

Creating a Report Template Example

Step 7) Entering prefix or pattern.

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

Creating a Report Template Example

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.

Creating a Report Template Example

Step 9) Click on the “Next” button.

Creating a Report Template Example

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

Creating a Report Template Example

The report will be generated as shown in below screen.

Creating a Report Template Example

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:

  • Programmatic compilation.
  • Compilation using Apache ANT task.

Understanding the different report bands

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

  • Title
  • Page header
  • Column header
  • Detail
  • Column footer
  • Page footer
  • Summary

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:

  • Core changes – The business changes or enhancements. It is required to change the core logic of the report.
  • Complicated reports – sub-reports and cross-tabs reports are good’ example.
  • Charts reports or Visual charts, for example, Graph, XY line, Pie, Bar, Time series, and Meter charts.

Disadvantages of using Jasper Reports

Here, are drawbacks/cons of using Jasper Reports.

  • Once you start making any change with parts of the package and removing features, it’s very difficult to get back to the default package setting.
  • It’s too rigid, editing of lines and creation of cross tabs is a challenging task.
  • Some common task, like specifying sub-totals for a grouped report is easier in the other two report designers.
  • Jasper requires that you compile reports before running them. This means that report developers must have the entire Java SDK installed.
  • Sometimes when you edit the report, it does not show the same preview as it is displayed on the web.

Summary

  • JasperReports is an open-source Java reporting engine. This Java class library offers reporting capabilities for your application.
  • Jasper Reports offers report generation performance and delivery to users.
  • Jasper Reports life cycle include 1) designing reports, 2) Compiling reports, 3) Executing the report, and 4) Exporting the report to the desired format.
  • In Jasper Reports, you can create multiple data sources to transfer data.
  • The major difference between Jasper and Pentaho is that Jasper reports uses a report for designing the reports while Pentaho uses the Pentaho Report Designer.
  • The report template file used in JasperReports is a simple XML file having jrxml extension.
  • Important Reports bands are: 1)Title 2) Page header 3) Column header 4) Detail 5) Column footer 6) Page footer and, 7) Summary.
  • The biggest challenge of using Jasper Reports is that you need to change the core logic of the report if the business logic change.
  • The biggest drawback of Jasper Reports is that when you make any change with parts of the package, it’s tough to get back to the default package setting.