Calling Jasper Report From Java Application

Posted on by By admin, in Business Intelligence, Jaspersoft, Open Source Business Intelligence | 0

Calling-jasper-report-from-java-application

Considering reader has prior idea of creation of report in jasper reports. I am going to describe how we can run jasper reports from java application. First create report, and publish the report on jasperserver.

Make data easy with Helical Insight.
Helical Insight is the world’s best open source business intelligence tool.

Click Here to Free Download

Report name: report.jrxml. In the java code add all the jar files required to build the project from jasper installed directory.

lib-java

In java class write the following piece of code:
package local;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;

public class JasperReportFillI18 {

public static void main(String[] args) throws JRException, InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {

String sourceFile = args[0]; //jrxml file path
String locale = args[1];//language
String outputDirectory = args[2];//output file path directory
String outputFileName = args[3];// output file path directory with  file name
JasperReport jp = JasperCompileManager.compileReport(sourceFile);
// calling exportReportToPdf method
//exportReportToPDF(jp, outputDirectory, outputFileName, locale);
File outDir = new File(outputDirectory);
outDir.mkdirs();
JasperPrint jasperPrint = null;
Connection con = null;
// Export to PDF.

try {
//database connection
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
Class.forName(“org.postgresql.Driver”).newInstance();
Connection datasource = DriverManager.getConnection(
“jdbc:postgresql://localhost:5433/foodmart”, “postgres”,
“postgres”);

HashMap parameterMap = new HashMap();
//Preparing parameters
parameterMap.put(JRParameter.REPORT_LOCALE, new  Locale(locale));
//parameterMap.put(“year”, 1961);
//parameterMap.put(“DataFile”, “CustomTableModel.java”);
jasperPrint = JasperFillManager.fillReport(jp, parameterMap,
datasource);

//for empty datasource
 //jasperPrint = JasperFillManager.fillReport(jp, parameterMap, new JREmptyDataSource());
JasperExportManager.exportReportToPdfFile(jasperPrint,
outputFileName);

} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(“Job has finished::!!”);
}
}
Configure the run time arguments in run configurations consider the following example:
“E:/Ireport Reports/local_test.jrxml””en_US””E:/Ireport Reports””E:/Ireport Reports/test.pdf”
Execute the java application to see the final output in pdf format in the path provided in arguments.

Make data easy with Helical Insight.
Helical Insight is the world’s best open source business intelligence tool.

Grab The 30 Days Free Trail

logo

Best Open Source Business Intelligence Software Helical Insight is Here

logo

A Business Intelligence Framework

Thanks
Nisha Sahu
BI Developer

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments