How do I implement custom java code / scriptlets?


I am using iReport Designer 5.1.0.  I have been reading up on scriptlets and implementing custom java code, but I cannot seem to make it work.  The problem I want to solve is:

I have a db connection and can access whatever I want just fine.  However, there is a field that is a comma separated String which I want to do some operation on that is more complex than is supported by iReport, and then feed the results back into the report.  More specifically, this field is a set of Strings (one String per entry in the db) that I want to feed in to a custom class.  This class will do some operation over every String examined, and return a List to the report with the overall operation results.

Writing the custom Java is an issue, but I need to know what links to what between the custom class and iReport.  For example, where do I register the input value and the output value for this oepration.

The steps I have taken so far are:

1.  Write the custom class extending JRDefaultScriptlet, export as a JAR

2.  Add the JAR to the classpath of iReport

3.  Create a scriptlet with the same name as the class

4.  Define a new variable with the same name as the custom class output; change calculation type to System


What other steps do I need to take to get the result I want?  Is this even possible?


breback's picture
Joined: Jun 18 2013 - 11:38am
Last seen: 8 years 7 months ago

1 Answer:

I did not understand finally what you are planning to do with the content in iReport (the string list), but apart from that from my point of view you already have done everything correctly; It is not mandatory to use a variable in the report to access the results of your java class. In all expressions in the report you may access all public methods of your scriptlet directly. Of course, you can write the result into that variable in the scriptlet, too, then use the variable in the report, but you don´t have to.

A main point  would be, at which time the result is calculated in the scriptlet class and and which time you access these values in the report execution process.

If you want to print the list of strings from your scriptlet row-wise in your report, it would be a good idea to build a list datasource of them, then use this datasource with alist, table, crosstab, subreport, whatever.

Hope this helps.

Cheers, Thomas

Thomas Zimmer's picture
Joined: Oct 2 2012 - 1:35am
Last seen: 5 hours 50 min ago

Yeah I figured it out (at least the implementation part). I'll try the datasource idea, thanks!

breback - 9 years 8 months ago