Java | Simple Jasper Report Example


JasperReports library is an open source reporting library written in Java. The library can export documents in different formats e.g. PDF, HTML, CSV…etc. This post shows a simple example of how to export PDF document using JasperReports.

Environment, Tools & Libraries used in this post

  • Maven (build tool)
  • jasperreports 6.10.0
  • spring core 5.2.3.RELEASE



In order to generate a report we need to following inputs to be passed to Jasper exporting functions:

  • Jasper Report Template: Jasper template is an XML .jrxml file that can be created using JasperSoft Studio.
  • Parameters: This is a Java Map object containing a set of values passed from the application requesting the report. They can be used for runtime configuration or to pass additional custom data that is not part of the data source.
  • Datasource: JasperReport engines expect an object of type JRDataSource as a data source. There several implementations of JRDataSource that allow to fetch data from different data source types such as databases, XML file, CSV file or Java beans.

In this post will export a PDF file using a List of Java bean Country as a data source. The template we are going to use report.jrxml is created using JasperSoft Studio.




    <!-- -->


Java Bean

  • We have one simple Java class Country that holds country’s code, name and URL to an image of the contry’s flag.
public class Country {

    private String code;
    private String name;
    private String url;

    public Country(String code, String name, String url) {
        this.code = code; = name;
        this.url = url;


  • The class contains the main() method.
  • The main() method call 3 custom methods
    1. getJasperReport() read & compiles .jrxml file.
    2. getParameters() returns the set of parameters.
    3. getDataSource() builds the data source .
package com.hmkcode;

import net.sf.jasperreports.engine.*;
import org.springframework.util.ResourceUtils;

import java.util.*;

public class App 
    // name and destination of output file e.g. "report.pdf"
    private static String destFileName = "report.pdf";

    public static void main( String[] args ) throws FileNotFoundException, JRException {

        System.out.println( "generating jasper report..." );

        // 1. compile template ".jrxml" file
        JasperReport jasperReport = getJasperReport();

        // 2. parameters "empty"
        Map<String, Object> parameters = getParameters();

        // 3. datasource "java object"
        JRDataSource dataSource = getDataSource();

        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, 
        JasperExportManager.exportReportToPdfFile(jasperPrint, destFileName);


    private static JasperReport getJasperReport() throws FileNotFoundException, JRException {
        File template = ResourceUtils.getFile("classpath:report.jrxml");
        return JasperCompileManager.compileReport(template.getAbsolutePath());
    private static Map<String, Object> getParameters(){
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("createdBy", "hmkcode");
        return parameters;

    private static JRDataSource getDataSource(){

        List<Country> countries = new LinkedList<>();

        countries.add(new Country("IS", "Iceland", 

        // 9 other countries in GITHUB

        return new JRBeanCollectionDataSource(countries);

Source Code @ GitHub