views:

529

answers:

3

Hi, Have anyone tried to import user defined classes in jasper report (.jrxml file)? I want to use some (user defined) Util class inside my jasper report to cook some bean attributes. I am using Javabean datasource

Please let me know if you need further clarification.

syntax to import class is

<import value="java.util.HashMap"/>

I want to use

<import value="mypackage.MyUtil" />
 ....
 ....
<field name="myVar" class="java.lang.String">
    <fieldDescription><![CDATA[MyUtil.cook(myData)]]>
    </fieldDescription>
</field>

The simple definition for MyUtil.java could be

package mypackage;
public class MyUtil
{
    public static String cook(String data)
    {
        return data + "_cooked";
    }
}

Thanks Nayn

A: 

You have to set the classpath in your iReport. It depends on its version, but is generally under Settings/Classpath

Bozho
I am not using iReport.
Nayn
well, then it should just work. What does the compiler tell you?
Bozho
net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : WireServiceUtil.getLinkedWires(wireRelationships) ....Caused by: java.lang.NoSuchMethodException: Unknown property 'WireServiceUtil'.To put further light on this, 'WireServiceUtil' is contained in another jar than the one where jrxml resides. But it is on classpath
Nayn
That's odd. it should work. Try importing the WireServiceUtil in the class where you compile. Also check if the variable you are passing to the method really exists.
Bozho
The jrxml files are inside a jar? how are you compiling them?
medopal
Mine is a web application, where jar gets exploded in tomcat, the jrxml compilation happens at run time.>> JasperCompileManager.compileReportToFile(subReport)
Nayn
jars getting exploded in tomcat is something strange. :)
Bozho
+1  A: 

I think I should have tried sufficiently before asking this.

There is nothing extra needed apart from There are two sections in jrxml: 1. Defining fields from javabean source 2. Using fields defined in step 1. to populate values in detail band

I was trying to cook the value of javabean members even before they are used to create fields So, jasper was trying to parse that 'expression' as javabean member.

Following is wrong

<field name="myVar" class="java.lang.String">
     <fieldDescription><![CDATA[MyUtil.cook(myData)]]>
     </fieldDescription>
</field>

When I used the Util class on field value, it worked.

<textField>
    <reportElement x="200" y="0" width="100" height="13"/>
    <textElement/>
    <textFieldExpression class="java.lang.String">
         <![CDATA[MyUtil.cook($F{myVar})]]>
    </textFieldExpression>
</textField>

Thanks Nayn

Nayn
+1 for self learning. In cases like this you see how much better it is to use iReport.
medopal
yeah.. I love iReport. But unfortunately it doesn't work for my javabean datasource. For some unknown reasons, it doesn't allow me to create javabean datasource. "General exception : Null" is what i get. My java beans have field enum inside it apart from normal getter/setter for fields. The fields enum is used similar to java reflection to access field names programmatically wherever required. It is faster than reflection.
Nayn
Ehm, perhaps you can ask another question for the datasource problem ? It is a way better approach to have things pre-compiled (in case you don't generate them dynamically)
Bozho
A: 

Can any body help how to import user define class in jrxml it is not working in my case showing some eroor. pls give me in detail of jrxml

abani
Could you provide the error details. In case the error is ClassNotFoundExceotion, then ensure you have added the classes in the classpath. Is the error about accessing class methods? In that case below answer suffice.
Nayn