Call Jasper Report / Ireport From Java Application

Call Jasper Report / Ireport From Java Application Example describes about How to call Jasper Report and Ireport from a java application.

Jasper-Reports is a free reporting engine that could be written to screen, or to a printer or as HTML, PDF, RTF, Microsoft Excel, XML files, CSV format and ODT files.

Jasper-Report is using in Java related applications, including Web and Enterprise applications, to develop dynamic reports. It reads its instructions from jasper file (compiled) or XML dynamically

We can generate reports using two ways,

1) From "JRXML" (Source) file
2) From "Jasper" (Compiled) file
 

Tools Needed For This Tutorial

You need to download

  1. JDK 6
  2. iReport-4.1.1 for designing the report

Following jar must be in classpath (Available from ireport installation directory)

  1. commons-beanutils-1.8.2.jar
  2. commons-collections-3.2.1.jar
  3. commons-digester-1.7.jar
  4. commons-logging-1.1.jar
  5. groovy-all-1.7.5.jar
  6. iText-2.1.7.jar
  7. jasperreports-4.1.1.jar
Note

If you are created a JRXML/Jasper file using a particular version of Ireport (For example consider Ireport-4.7.0)

In order to run the report, you must use same set of jar's available on installed Ireport-4.7.0 directory (C:\Program Files\Jaspersoft\iReport-4.7.0\ireport\modules\ext)

If you are creating a report using Ireport-4.7.0 and compiling / running using Ireport-4.2.0 jar files, you will get java.lang.NullPointerException

Create PDF Report From JRXML File

JRXML file is a JasperReports Document. JRXML is the XML file format of JasperReport, which can be coded manually, generated, or created using a tools like IReport, JasperAssistant etc

Execution of report from JRXML file will be very slow, as it need to compile before the execution

import java.io.IOException;
import java.util.HashMap;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

// Java Program To Call Jasper Report

public class PdfFromXmlFile {
 
public static void main(String[] args) throws JRException, IOException {
   
   
JasperReport jasperReport = JasperCompileManager.compileReport("report.jrxml");
    JasperPrint jasperPrint = JasperFillManager.fillReport
(jasperReport,new HashMap(), new JREmptyDataSource());
    JasperExportManager.exportReportToPdfFile
(jasperPrint, "sample.pdf");
 
}
}

Create PDF Report From Jasper File

Jasper file is a compiled format of JasperReports Document.

Execution of report from Jasper file will be very fast, as it is pre-compiled

It is recommended for the production environment

import java.io.IOException;
import java.util.HashMap;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

// How To Invoke Ireport From Java Application

public class PdfFromJasperFile {
 
public static void main(String[] args) throws JRException, IOException {

   
JasperPrint jasperPrint = JasperFillManager.fillReport("report.jasper"new HashMap<String, Object>(),
   
new JREmptyDataSource());
    JasperExportManager.exportReportToPdfFile
(jasperPrint, "sample.pdf");

 
}
}





Comments (14)
10.07.2012 04:18:42 pradhumna
hi 
could you let me know what are report.jrxml, report.jasper,sample.pdf ? 
Are they predefined R our own files inthis program /
11.07.2012 04:21:21 Coolashy1510
Hi
I tried copying the same and changed only the name of the jasper file. yet there is an exception..

Exception in thread main java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at net.sf.jasperreports.engine.util.JRLoader.(JRLoader.java:61)
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:536)
    at MyReportViewer.main(MyReportViewer.java:13)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 3 more


please help 
12.07.2012 03:19:22 admin
@pradhumna report.jrxml file is created using Ireport designer, report.jasper is a compiled format of report.jrxml, if you have both then you can generate pdf according the design template (report.jrxml)

@Coolashy1510 you need to have commons-logging-1.1.jar in your classpath
12.07.2012 04:23:12 Sherin Matheew
Hi,
Have checked if you have all the below mentioned jar file in  the lib folder of your server
1.commons-beanutils-1.8.2.jar2.commons-collections-3.2.1.jar3.commons-digester-1.7.jar4.commons-logging-1.1.jar5.groovy-all-1.7.5.jar6.iText-2.1.7.jar7.jasperreports-4.1.1.jar
24.08.2012 21:33:30 Swapnil Shirsat
I got those jars but still i m getting exceptions. but i am using ireport 4.7.0 version
25.08.2012 02:33:58 admin
What exceptions are you getting? can you provide exception stack?
25.08.2012 04:33:02 Swapnil Shirsat
Those whose code is running can u give the path from where you got all this jars
13.09.2012 04:04:30 DeD
Hi all; 
I tried it, I downloaded all the .jar files but i'm still getting exceptions :

xception in thread main java.lang.NullPointerException
    at net.sf.jasperreports.engine.JRPropertiesMap.readObject(JRPropertiesMap.java:185)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1848)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1946)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1870)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
    at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1666)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1322)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1946)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1870)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
    at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1666)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1322)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1946)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1870)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
    at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:88)
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:515)
    at javaapplication2.JavaApplication2.main(JavaApplication2.java:16)
 
15.09.2012 03:23:07 admin
@Swapnil Shirsat
@DeD

If you are created a report using Ireport-4.7.0
you must use the same jar files available from installed Ireport-4.7.0  directory

All jar available on this folder
C:\Program Files\Jaspersoft\iReport-4.7.0\ireport\modules\ext

you can't use iReport-4.7.0 with old jar files, in that case you will get java.lang.NullPointerException
17.09.2012 04:01:25 Hardik Mishra
Hey This is really helpful. I have used this. In my case I am passing Map with parameters and .jrxml have those parameters defined. But, I am getting blank PDF.
19.09.2012 04:02:35 admin
Without checking .jrxml, I cant say the problem, can I get your jrxml file, you can sent to 
info@javatips.net
26.09.2012 03:58:39 haris
pls send that jar files link
27.09.2012 03:57:42 admin
if you installed Ireport, you will get all the jar's inside below folder
C:\Program Files\Jaspersoft\iReport-4.7.0\ireport\modules\ext 
22.11.2012 05:07:52 somphas
i got jassper-4.7.1.jar and m using ireport-4.7.1 too but it still got this message  Exception in thread AWT-EventQueue-0 java.lang.NullPointerException what's the problem ???