Jaspersoft BI Tools

iReport,Jaspersoft Design Studio,Jaspersoft Server, Schema Workbench,Analysis,Dashboards, Self Service BI, Domains,Topics,Ad-Hoc Reports, Jaspersoft ETL, Embedding BI,HTML-5 charts, Fusion Charts,JFree Charts,Drill down reports , Drill down Dashboards,Visualize.js, Performance Tuning, D3 Charts, Jaspersoft Customization

Tuesday, January 12, 2016

Scriptlet Example in Jaspersoft 6.x for begineers - Hello World Sample Example

Hi Folks,
This tutorial talks about step by step procedure to write a custom Scriptlet in Jasper Design Studio

Environment : 
Jasper Design Studio 6.2 Professional ,
Eclipse Mars.1 Release (4.5.1)

Aim of the tutorial : Print "Hello..." information on summary band. 
1) Choose your Java Editor 
        (I have taken Eclipse for this tutorial user friendly for Eclipse
            - Netbeans is developer friendly for iReport Designer)

2) Create Java Project 
     File -> New -> Project -> Java Project -> Give Project Name as "Test"

3) Add Jasper Server jar file to Project Path
Right click on Project Name -> Build Path ->Configure Build Path -> Libraries -> Add External Jar Files -> Locate the jar file from below location -> Click on Apply -> OK
   Location of Jar File : 
      C:\Jaspersoft\jasperreports-server-6.2.0\apache-tomcat\webapps\jasperserver-pro\WEB-INF\lib
   Jar File Name : jasperreports-pro-6.0.0.jar

4)  Create a Package & a Class inside it with your business logic

Package name is : com.sadakar.jasper
Class Name is : Test4

package com.sadakar.jasper;
import net.sf.jasperreports.engine.JRDefaultScriptlet;
import net.sf.jasperreports.engine.JRScriptletException;

public class Test4 extends JRDefaultScriptlet{

    public String hello() throws JRScriptletException
    {
        return "Hello! I'm the report's scriptlet object.";
    }
}





NOTE :
 Scriplets can be written using  JRDefaultScriptlet  and  JRAbstractScriptlet
The current example is a user defined scriptlet hence it should import JRDefaultScriptlet
More information can be found at : http://jasperreports.sourceforge.net/sample.reference/scriptlet/

We can not run  the class as Java Application. To run it as a Java Application it requires main() method.

5) Make the Project as Jar File (Export the project as a Jar File)
Right click on the "Test" Project -> Export -> Java (JAR File). 
 I have given jar file name as "test"

6) Make use of Custom Java Code from the exported Jar File in Studio  - How ?  

a) Put the Jar in Studio Project Path 
   Right Click on "My Reports" Project -> Build Path -> Configure Build Path -> Libraries -> Add External Libraries ->("test" jar from your exported location from Eclipse) ->

b) Creating a sample JRXML to test the hello() method from the JAVA Code
     i) Create a JRXML
     ii) Remove unnecessary bands
     iii) Go to "Scriptlets" section from "Outline" window
     iv) Right Click on "Scriplets" and then  click on "Create Scriptlet"
     v) Give Name="Hello" and Class="com.sadakar.jasper.Test4"  (with out double quotes).
     vi) Observe that as quick as you create Scriptlet, Jasper Studio engine creates a parameter
           in  "Parameters" node. Here it will create "Hello_SCRIPTLET"
     vi) Now, call the hello() method anywhere in the report
           Drag a "Text Filed" to the summary and write this expression
                                            $P{Hello_SCRIPTLET}.hello()


 NOTE : If you don't use any data source to the report select No Data Type = "All sections No Detail" to the report in its properties

 7) Save , Compile and run the report. 




Download Examples : 
JRXML+JAR File + Complete Eclipse Project 

I hope this helps some one to get start with scriptlets in jasper

References : 
http://jasperreports.sourceforge.net/sample.reference/scriptlet/
http://blog.sacaluta.com/2007/07/jasperreports-scriptlet-example.html