Create Jasper Reports and deploy as a JAR

Shameer Ahmed
Geek Culture
Published in
2 min readJul 14, 2021

Jasper reports is an open-source reporting framework that helps in building easy to complex level reports providing a variety of formats such as PDF, Excel, CVS, and TXT files. It is a Java library that can be used in any type of Java Application and you can build them using Jasper Studio or studios that support Jasper reports building. By using these tools it becomes easy to build reports as it provides drag and drop support. In this article, we will explore how to pass parameters, fields in reports and subreports, and how to make deployable JAR files while working with jasper reports.

Singular values behave like key-value pairs and can be passed as parameters in the report and subreports. fields, on the other hand, behave like a list of elements that can also be passed to subreports.

Now let's get into code, to make sure that Jasper files work on JAR it has to be taken as a Resource as Stream which is set as Input Stream. Resource As Stream takes classpath of the file location and it keeps information after creating JAR the location where the file will be found.

ClassLoader classLoader = Constants.class.getClassLoader();InputStream in = classLoader.getResourceAsStream
("Folder" + File.separator + filename + ".jrxml");

JasperReport jasperReport = JasperCompileManager.compileReport(in);

As you can see in the code File.separator is used. It helps in setting backslash and slashes based on the environment either if it is deployed on Windows or Linux making sure that the file can be found on either environment.

To pass parameters, map object is used described as follows:

Map<String, Object> parameters = new HashMap<>();
JasperReport jasperSubReport = loadReport("fileName.jasper");
parameters.put("key", "value");
parameters.put("key", "value");
parameters.put("SUBREPORT_INPUT_STREAM", jasperSubReport);

In the jasper file following changes are needed:

<parameter name="SUBREPORT_INPUT_STREAM" class="java.lang.Object" isForPrompting="false"/><subreport>
<reportElement x="25" y="0" width="170" height="30" uuid="a0373767-848a-44ea-b6eb-80128fa60453"/>
<dataSourceExpression>
<![CDATA
[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource
($F{outputs})]]>
</dataSourceExpression>
<subreportExpression class="java.io.InputStream"><![CDATA[$P{SUBREPORT_INPUT_STREAM}]]></subreportExpression>
</subreport>

To pass Fields in Jasper List Object is passed as follows:

JRBeanCollectionDataSource dataSource = 
new JRBeanCollectionDataSource(list);

Now setting up all parameters in the jasper file as follows:

JasperPrint jasperPrint = JasperFillManager.fillReport
(jasperReport, parameters, dataSource);

Now for the export of the file following code is utilized:

JRXlsExporter jrXlsExporter = new JRXlsExporter();
jrXlsExporter.setExporterInput(new SimpleExporterInput(jasperPrint));
jrXlsExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(file+ {{format}})); // e.g# .xls
jrXlsExporter.exportReport();

The above guidelines summarize how to set up the export of Jasper reports deployable on JAR. Passing Data through Parameters and Fields in Jasper using Java and passing data through the main report to subreports.

--

--

Shameer Ahmed
Geek Culture

a Tech savvy with a curious mind, having curiosity in entrepreneurship, business, and personal development. https://shameerahmad.me/