René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

Jasper - Formulas (calling Java functions)

A Java Function Ex_10_00.SumSum, found in Ex_10_00.java, can be called. The function/class must be embedded into a jar file, then, the jar file must be added in iReports with Extras->Options->Classpath.
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport 
  xmlns="http://jasperreports.sourceforge.net/jasperreports" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" 
  name         ="report name" 
  pageWidth    =        "595" 
  pageHeight   =        "845"
  columnWidth  =        "595" 
  leftMargin   =          "0" 
  rightMargin  =          "0" 
  topMargin    =          "0" 
  bottomMargin =          "0"
>
  
	<queryString> <![CDATA[

    select 1 val_1, 1 val_2 from dual union all
    select 1 val_1, 5 val_2 from dual union all
    select 5 val_1, 9 val_2 from dual

  ]]> </queryString>

	<field name="VAL_1" class="java.lang.Integer"/>
	<field name="VAL_2" class="java.lang.Integer"/>

	<detail>
		<band height="13" splitType="Stretch">
			<textField> <reportElement x=  "0" y="0" width="100" height="13"/> <textElement/> <textFieldExpression class="java.lang.Integer"><![CDATA[$F{VAL_1}]]></textFieldExpression> </textField>
			<textField> <reportElement x="100" y="0" width="100" height="13"/> <textElement/> <textFieldExpression class="java.lang.Integer"><![CDATA[$F{VAL_2}]]></textFieldExpression> </textField>
			<textField> <reportElement x="200" y="0" width="100" height="13"/> <textElement/> <textFieldExpression class="java.lang.Integer"><![CDATA[Ex_10_00.SumSum($F{VAL_1}, $F{VAL_2})]]></textFieldExpression> </textField>
		</band>
	</detail>

</jasperReport>
Ex_10_00.java
// Compiling this file:
//   javac Ex_10_00.java
//
// Creation of the jar file:
//   jar cvf Ex_10_00.jar Ex_10_00.class
//
public class Ex_10_00 {

 static public java.lang.Integer SumSum(
    java.lang.Integer a, 
    java.lang.Integer b
  ) {  
      java.lang.Integer r = 0;

      for (java.lang.Integer i=a;i<=b;i++) {
        r+=i;
      }
      return r;
    }
}
After compiling the Java source and creating the jar file, the jar file must be addin in iReports with Extras->Options->classpath