Jump to content
We've recently updated our Privacy Statement, available here ×

Can we use java functions in ireport


venujasper

Recommended Posts

  • Replies 11
  • Created
  • Last Reply

Top Posters In This Topic

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)

Link to comment
Share on other sites

  • 1 year later...

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

Link to comment
Share on other sites

  • 1 year later...

 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

 

 

 

 

Link to comment
Share on other sites

  • 1 year later...

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

Link to comment
Share on other sites

  • 2 years later...

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? 

Link to comment
Share on other sites

  • 11 months later...
  • 7 months later...

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} )

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...