Can we use java functions in ireport

0

Hi ,

         I am using ireport tool in web application to generate  reports.There is a requirement to call java function from ireport tool.Is there any way to call java function from ireport. Any idea?

Thanks,

Kitti

venujasper's picture
Joined: Jan 6 2009 - 10:23pm
Last seen: 6 years 3 months ago

10 Answers:

0

First iReports is the report designer. Jasper Report is the reporting engine.

When you say Java function what do you mean? Do you mean your own custom Java Class? What are you doing in this Class?

svenn's picture
1393
Joined: Mar 19 2007 - 5:57am
Last seen: 12 years 10 months ago
0
Hi, Yes I mean custom java class,I need this to encode and decode the fields.i need to pass the fields to the custom java class and get the decoded format of fields.Can i do this using ireport tool. Regards, Kitti
venujasper's picture
Joined: Jan 6 2009 - 10:23pm
Last seen: 6 years 3 months ago
0

Yes, build you java class, put it in a jar, and then ref the object in your jasper wherever yo want to use it's methods.

example:

Create class Decoder that just converts lower to upper case

package dummy.test;

public class Decoder {
 
 public String decode(String input) {  
  return input == null ? "" : input.toUpperCase();  
 } 
 
}

Create a jar containing that class, and place on classpath.

Create a blank report an put one text field on the report with the following expression as it's value.

new dummy.test.Decoder().decode("this was all lower case")

The report will show the converted text "THIS WAS ALL LOWER CASE"

Couldn't get this to work with a static method for some reason, but didn't try real hard.

You can also create a parameter in your report of type dummy.test.Decoder with the default value = new dummy.test.Decoder()

Then whenever you want to use the method in a text box, etc. just use expression $P{PARM_NAME}.decode(strValue)

This allows you to new the Dummy class once and use in multiple places in your report (or even pass as a param to a subreport)

djsiders's picture
Joined: Nov 17 2006 - 8:30am
Last seen: 3 weeks 11 min ago
0

You should use static methods to avoid to instance a new class all the times you need to call your function.

Be sure you class is in the classpath.

giulio's picture
29620
Joined: Jan 2 2007 - 4:15pm
Last seen: 11 months 3 weeks ago
0

Thank you very much.This is very usefull.

 

Thanks & Regards,

Kitti

venujasper's picture
Joined: Jan 6 2009 - 10:23pm
Last seen: 6 years 3 months ago
0

I am trying to create report using ireport(3.5.1) & i want to use Custom class. where my custom class reading file & returning a string
like
package org.test.com;
public class ReadUrlFile {
   
    public String getInputUrls()
    {
    //reading file....
    return urls;
}

for this i have created java class, made a jar, and then i have added text fied in report & refer the Class in jasper to use it's methods.
like new org.test.com.ReadUrlFile().getInputUrls()
i also tried like (new org.test.com.ReadUrlFile()).getInputUrls()

I have added jar in ireprt classpath from : Tools > option > classpath.

when i click on preview  i am getting ClassNotFoundException

Anyone help me to come out.
what i have missing

Thanks,
awadhai

awadhai's picture
Joined: May 20 2010 - 5:17am
Last seen: 9 years 8 months ago
0

 yes we can call java methods from jasper reports. Just Follow the following steps

 

Step1:

create a java class with a static method which return something i.e either String or Integer.. 

 

Step2:

Create a jar file by command jar - cvf file.jar packagename

 

Step3:

set it as class path as 

Tools > Options > ClassPath > Add Jar > Ok

 

Step4: 

Create a Static field. Edit its Expression by EditExpression to  "new packagename.classname().methodname(parameter) "

Step5:

Place this static field wherever you want

 

 

 

 

iamlegend108's picture
Joined: Dec 7 2010 - 2:53am
Last seen: 9 years 1 month ago

Hi,
Is this the same as an scriplet or no need to be like that? I've been trying for days to do this but no luck...searched for info all these days and couldn't find a good sample with code. If any of you have a guide please would be awesome to share it.
Thanks

cavagnaro.jc - 6 years 6 months ago
0

I get compile error "GetFileLoc.getLoc cannot be resolved to a type" and "Errors were encountered when compiling report expressions class file". 

Java is this:

package testJSS;
import java.io.File;
public class GetFileLoc {
public static String getLoc(){
File currentDir = new File("").getAbsoluteFile();
String loc = currentDir.toString();
return loc;
}
 
public static void main(String[] args){
// String loc = GetFileLoc.getLoc();
// System.out.println(loc);
}
}// close class

Text Field expression in Report is this: (new GetFileLoc.getLoc())

Went into Project > Properties > Java Build Path > Libraries and added the External Jar. 

Any ideas? 

therrick_1's picture
Joined: Jul 17 2015 - 6:48am
Last seen: 4 years 5 months ago
0

Try to specify the full name of the component (package.componentname.method).

It looks like you don't provide the package name.

Set the expression as the following:

new testJSS.GetFileLoc.getLoc()

kossonou_landry's picture
Joined: Jun 15 2016 - 10:57am
Last seen: 2 years 7 months ago
0

For a customer we need to decrypt stored data to show on the Jaspersoft report using a JAVA function that performs the decryption.

The report language is set to "groovy", used version Jaspersoft report server 6.2. It works on older versions like 4.7.1 as well, as done earlier in the project.

The utils.java.helpers.jar is put in both the classpath of Jaspersoft Studio (or iReport when JS 5.5 or older is used) and in the lib folder of the deployed WAR file in Tomcat.

To call a JAVA function, create a field and add an expression like the exxample below:

new GroovyClassLoader().parseClass('''
    def static decryptCPR(String cpr, String meteringPoint, String envKey)
    {
      
        String decryptedKey = utils.java.helpers.CryptoLib.decryptCPR(cpr, meteringPoint, envKey);
 
        return decryptedKey;
    }
''').decryptCPR($F{FIRST_CONSUMER_CPR}, $F{MPO_METERING_POINT_ID}, $P{P_ENV_KEY} )

 

g.den.breejen's picture
Joined: Apr 25 2013 - 4:53am
Last seen: 5 months 1 week ago
Feedback
randomness