Basics – JasperReport on your Java Desktop App


Have you ever had a client that only wants a basic reporting screen that has the values in it? Good thing JasperReports was invented!

In this post, I’m going to share a small example of how you can create your own report in iReport, compile it to *.jasper and call them via your own Java Desktop App!

Step by Step

Create your report in iReport

You can actually code this in XML but that would be inefficient. iReport is an open source report editor that developers can use to design reports. Jasper Reports are basically like Java Source code that you can compile in different language, as of this writing, it supports Java (of course), Groovy and JavaScript. Obviously, if you want to create a report that can be viewed in the Java based report viewer, you should compile it in Java. If you want to view it in a Web Browser, it should be in Javascript (although, technically, you can wrap a JFrame in an Applet and place the JasperReport viewer on the Applet, but not getting ahead in here).

In this example, let’s just create a simple Report (a blank perhaps) and put a few parameters and fields.

Screenshot at Jun 15 22-20-20

Once done, compile it and locate your jasper file (*.jasper is the compiled source file).

Code it!

I wouldn’t dive much on how you where you should put your jasper files. It can be externally access or via resource classpath. My recommendation is of course, put it in your classpath, but we all have our own case of usage. If there will be a lot of adjustments to your report because of alignments, then I would go with putting it externally. This is so that you wouldn’t have to compile your project again just to do minor alignments.

Before we go to the snippet code, make sure you have the dependency below.


If you’re not familiar with Maven, you should be, read here. 🙂

and here is a sample of code.

// Get the file, put it in a stream
FileInputStream fis = new FileInputStream(this.getClass().getResource("").getFile());
BufferedInputStream bufferedInputStream = new BufferedInputStream(fis);
// We do have parameters, so we create the map object to store values for them.
Map map = new HashMap();
map.put("SAMPLE1", "This is a sample 1 text");
map.put("SAMPLE2", "This is a sample 2 text");

// We create the jasper report object, load the report with JasperFillManager object and use the
// JasperViewer to display the report            
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(bufferedInputStream);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map,new JREmptyDataSource());
JasperViewer.viewReport(jasperPrint, false);

Screenshot at Jun 15 22-19-45

If you’re having issues, just let me know (via comments), I can help you out.