How to use Jasper Reports in JavaFX 8 – Sample Example Tutorial

By | December 20, 2016

Jasper Reports are used for showing information that need to be printed or saved as document file.

Use Cases:

– For printing Reciepts
– Bill/ Invoice
– Detailed Reports
– and so on..

If you have already developed some Java application/ Software using JavaFx and now need to print some data from the software, then this is the tutorial for you.

Jasper Reports are designed in the form of .jrxml files similar to .fxml files of javaFX.
We need to access these .jrxml files in java class and pass the data to be filled in the Jasper report using Java Objects(List) or through an SQL query.

Steps required to achieve this:

1) Install Jaspersoft Studio for creating .jrxml file using drag and drop.
2) Get Jasper Reports Library .jar files to be added in libs folder of your JavaFX project.
4) Design .jrxml file using JasperSoft Studio.
3) Write Java code to show .jrxml file as Report.

Install Jaspersoft Studio

You can either download Jaspersoft studio plugin for eclipse OR download Standalone Jasper soft Studio. We just need the .jrxml report files designed using either of the above.

1) Eclipse Plugin: Search JasperSoft in Eclipse MarketPlace and install the latest release.
jaspersoft studio javafx download plugin

2) Standalone JasperSoft Studio: Download

Get Jasper Reports Library

We need jasper library for java project. To get the library files. Download the Jasper Reports Library from here.

jasper Reports Library for Java download

Download the “” in the above screenshot.

After extracting the zip file you will get the project folder in which “dist” folder is of our concern.
jasper dist folder
There are 3 .jar files in this dist folder. Add all the three files to the libs folder of your javaFX project.
But these 3 jar files are not enough. There are many other dependencies require by Jasper Reports which should be again added to the libs folder of your project.

The Jasper Reports Project uses Apache Ant, Apache Ivy – as the dependency Manager. We need to get the Apache Ant/Ivy for getting the remaing .jar files.

We have to Download Apache Ant and run the commands on Jasper Reports Project’s root directory using Terminal/Command Prompt.

Download Apache Ivy, Apache Ant from here:
(You can alway get the latest releases by searching on Google)

Extract the downloaded .zip file and copy the folder ant anywhere near the Jasper Reports Project folder.
Directory structure of ant and jasper project

Now run the following command in the root directory of the Jasper Reports project:

java -jar ../ant/ant-launcher.jar retrievelibs

Look at the screenshot showing how I ran the above command:

terminal showing ant retrieving Jasper reports library for java

The required dependency .jar files will be downloaded into the dist/libs folder of the JasperReports project.
Copy all the .jar files from this lib folder to the libs folder of your java project.

jasper dependency jar files

NOTE: Add all the jar files to the Build Path of your Java project.
Here is the list of all the jar files that will be added to the libs folder of your JavaFX Project:

  1. ant-1.7.1.jar
  2. antlr-2.7.5.jar
  3. asm-all-4.0.jar
  4. barbecue-1.5-beta1.jar
  5. barcode4j-2.1.jar
  6. batik-anim-1.8.jar
  7. batik-awt-util-1.8.jar
  8. batik-bridge-1.8.jar
  9. batik-css-1.8.jar
  10. batik-dom-1.8.jar
  11. batik-gvt-1.8.jar
  12. batik-svg-dom-1.8.jar
  13. batik-svggen-1.8.jar
  14. batik-util-1.8.jar
  15. bsh-2.0b4.jar
  16. castor-core-1.3.3.jar
  17. castor-xml-1.3.3.jar
  18. commons-beanutils-1.9.0.jar
  19. commons-collections-3.2.2.jar
  20. commons-digester-2.1.jar
  21. commons-javaflow-20160505.jar
  22. commons-lang-2.6.jar
  23. commons-logging-1.1.1.jar
  24. core-3.2.1.jar
  25. ecj-4.3.1.jar
  26. groovy-all-2.4.5.jar
  27. hibernate-core-3.3.2.GA.jar
  28. httpclient-4.3.4.jar
  29. httpcore-4.3.2.jar
  30. icu4j-57.1.jar
  31. itext-2.1.7.js5.jar
  32. jackson-annotations-2.1.4.jar
  33. jackson-core-2.1.4.jar
  34. jackson-databind-2.1.4.jar
  35. jasperreports-6.3.1.jar
  36. jasperreports-fonts-6.3.1.jar
  37. jasperreports-javaflow-6.3.1.jar
  38. jaxen-1.1.1.jar
  39. jcommon-1.0.23.jar
  40. jfreechart-1.0.19.jar
  41. jxl-2.6.10.jar
  42. lucene-analyzers-common-4.5.1.jar
  43. lucene-core-4.5.1.jar
  44. lucene-queryparser-4.5.1.jar
  45. mondrian-
  46. olap4j-
  47. persistence-api-1.0.jar
  48. poi-3.10.1.jar
  49. poi-ooxml-3.10.1.jar
  50. rhino-1.7.6.jar
  51. servlet-api-2.4.jar
  52. spring-beans-2.5.jar
  53. spring-core-2.5.jar
  54. velocity-1.7.jar
  55. xalan-2.7.2.jar
  56. xml-apis-ext-1.3.04.jar
  57. xmpcore-5.1.1.jar

Design .jrxml file using JasperSoft Studio.

Open Jasper Studio and create a new Jasper Report as shown below:

Creating new Jasper Report

Now select Blank A4 as the Report Template as shown below:

Jasper Report Template

Choose the name and directory for the jrxml file and at the screen asking for Data Source, select “New Data Adapter – Empty Rows” from the drop down as shown below:

Selecting data source for jasper report

Click Next->Finish to create the jrxml file. Look at the screenshot below:

Jasper new reports

You can download the sample .jrxml file here: Blank_A4.jrxml
Create a folder named data in your java project and Copy the created .jrxml file there.
Now create a java file named as follows:

package com.marothiatechs.reports;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JFrame;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.swing.JRViewer;

public class PrintReport extends JFrame {

	private static final long serialVersionUID = 1L;

	public void showReport() throws JRException, ClassNotFoundException, SQLException {

		String reportSrcFile = "data/Blank_A4.jrxml";

		// First, compile jrxml file.
		JasperReport jasperReport = JasperCompileManager.compileReport(reportSrcFile);
		// Fields for report
		HashMap<String, Object> parameters = new HashMap<String, Object>();

		parameters.put("company", "MAROTHIA TECHS");
		parameters.put("receipt_no", "RE101".toString());
		parameters.put("name", "Khushboo");
		parameters.put("amount", "10000");
		parameters.put("receipt_for", "EMI Payment");
		parameters.put("date", "20-12-2016");
		parameters.put("contact", "98763178".toString());

		ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();

		JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(list);
		JasperPrint print = JasperFillManager.fillReport(jasperReport, null, beanColDataSource);
		JRViewer viewer = new JRViewer(print);
		this.setSize(700, 500);



Now call the source code for the Main.Java is as follows:


import java.sql.SQLException;

import com.marothiatechs.reports.PrintReport;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import net.sf.jasperreports.engine.JRException;

public class Main extends Application {

	Stage window;
	Scene scene;
	Button button;

	public static void main(String[] args) {

	public void start(Stage primaryStage) throws Exception {
		window = primaryStage;
		window.setTitle("Jasper Report Tutorial");
		button = new Button("Show Receipt");
		button.setOnAction(e -> {
			try {
				// --- Show Jasper Report on click-----
				new PrintReport().showReport();
			} catch (ClassNotFoundException | JRException | SQLException e1) {

		VBox layout = new VBox(10);
		layout.setPadding(new Insets(20, 20, 20, 20));
		scene = new Scene(layout, 300, 250);


Now Run the file as Java Application:
Jasper Report running in JavaFX JFrame

35,470 total views, 10 views today

(Visited 25,333 times, 1 visits today)
  • Michael

    Those field description in the jrxml sample should be removed and $F{date} should be set to java.lang.String type in order to have all param values displayed instead of “null”.

    • Laxman Marothiya

      Thanks Michael for the valuable comment

  • Saurabh Gangamwar

    net.sf.jasperreports.engine.JRException: dataBlank_A4.jrxml (The system cannot find the path specified)

    I am getting this error.please help

  • Rishi

    Very Nice And easy examples

  • Fourzerotree

    im getting error at this.add(viewer)
    Please help

  • Hrishav Dhawaj Purkayastha

    What if my javafx stage is full screen
    Does the JFrame shows on top of javafx stage

    I tried that and found that JFrame always hides behind the javafx stage and if tried to focus to the JFrame, it still hides behind

    Any ideas to overcome this issue?