Can anyone help me how to use scriptlets, i know we have to write java code to achieve certain conditions in designing report, but am confused a bit.
i need an example , like in which situation it is useful and an example scirptlet, it helps me a lot,
thanks to ur replies,
rgds,
smith.
8 Answers:
Hi,
I am trying to add a scriptlet to my report . I added tools.jar to my library . The report is compiled , but on executing it (using an active connection) , I am encountering an error . The name of my report is transactionReportMT , that of the scriptlet is transactionReportMTScriptlet and the method involved is testMethod()
This the error I get - java.lang.NoSuchMethodError : transactionReportMTScriptlet.testMethod(I)Ljava/lang/String;
Code: |
// Code for the method . This method is decalred just after the constructor of the //scriptlet class public java.lang.String testMethod(int i) throws JRScriptletException { Integer var = (Integer) this.getFieldValue("format"); if (var.intValue()==1) return "Type 1"; else return "Type 2"; }</td></tr></tbody></table> |
I just went through the scriplet hassel myself. How are you calling your scriptlet? First, I couldn't get my scriptlet to work from the "scriplets" section on iReport. I had to reference the class file from the "Scriptlet class" parameter on the report properties window.
1) Make sure you use the fully qualified name when referencing your class (e.g. com.myreports.myClass instead of myClass).
2) Import the following classes in your java code (you'll need to add the jasperreports-3.1.4.jar to your classpath)
import net.sf.jasperreports.engine.JRDefaultScriptlet;
import net.sf.jasperreports.engine.JRScriptletException;
extend the following class JRDefaultScriptlet
3) If not using the built in methods (e.g. afterReportInit() etc.), access your methods via the global report scriptlet like: $P{REPORT_SCRIPTLET}.testMethod(0)
That should do it. I'm new to JasperReports, but I feel pretty experienced now that I've had to put about 10 reports together for a new CRM installation. There are some wonderful features in the iReport/Jasper combo, but for a product that is as mature as this, there are too many bugs for my taste. I've spent more time troubleshooting parameter passing, variable grouping, text formatting, and scriptlet issues than I have creating actual reports.
Oh well, I guess I can't complain ... it is free after all! :)
Good luck,
Mike
Mike, where did you put you class file? I have a report named TestGraph.jrxml. I've created a java file and compiled a class file named TestGraphScriptlet.java/.class respectively. The fully qualified name is com.goldstar.scriptlets.TestGraphScriptlet. I've placed the .class file into the same directory as the .jrxml file and I'm getting an error:
java.lang.ClassNotFoundException: com.goldstar.TestGraphScriptlet
Am I supposed to put the class file somewhere else?
Thanks!
Yeah, I ran into the same problem ... I figured that by placing it in the same directory as the report files it would be picked up in the classpath. Unfortunately, that wasn't the case (not sure why). Anyway, within iReport you can add your jar file to the classpath. Go to Tools -> Options -> Classpath tab. Then click on the "Add Jar" button and reference your file.
That should do the trick. As a side note, my report are being used in SugarCRM (via ZuckerReports). When I uploaded my JAR with my report files, everything compiled just fine on the server (all in the same classpath). In other words, you could probably get your class files picked up by updating your machine classpath to include the location of your new jar file as well.
Good luck!
Mike
From Jaspersoft web site, in: Home » Exchange » JasperReports® Library download jasperreports-6.6.0-project.tar.gz.
Inside it under demo/samples/scriptlet are the following:
├── build.xml
├── docs
│ ├── defaultAnt.png
│ ├── index.xml
│ ├── jasperviewer.png
│ └── runServer.png
├── ivy.xml
├── readme.txt
├── reports
│ └── ScriptletReport.jrxml
└── src
├── ScriptletApp.java
├── Scriptlet.java
└── SimpleExportProgressMonitor.java
readme.txt says:
You need to have the Apache Ant build tool correctly installed on your system and the Apache Ivy dependency management plugin installed in Ant, to easily run this sample application.
The Apache Ant build tool can be downloaded from this location: http://ant.apache.org/
The Apache Ivy dependency management plugin can be downloaded from this location: http://ant.apache.org/ivy/Please follow their installing instructions to have Apache Ant and Apache Ivy working on your system before trying our sample application.
Once you have them installed, launch "ant -p" from the command line in this directory to learn more about what tasks you could run to build and test the sample.
On Debian the ant package comment is:
Apache Ant is a Java library and command-line tool whose mission is to drive processes described in build files as targets and extension points dependent upon each other. The main known usage of Ant is the build of Java applications. Ant supplies a number of built-in tasks allowing to compile, assemble, test and run Java applications. Ant can also be used effectively to build non Java applications, for instance C or C++ applications. More generally, Ant can be used to pilot any type of process which can be described in terms of targets and tasks.
And the ivy package comment is:
Ivy is a very powerful dependency manager oriented toward Java dependency management, even though it could be used to manage dependencies of any kind.
Install the above two packages. Then when you run ant -p as they tell you in the readme.txt you get:
Buildfile: /home/howard/Downloads/Jaspersoft/Jasper Reports/jasperreports-6.6.0/demo/samples/scriptlet/build.xml
Shows how the scriptlets could be used to manipulate data during report filling.
Main targets:clean Deletes all the generated files.
compile Compiles the XML report design and produces the .jasper file.
csv Generates a CSV version of the report by converting the .jrprint file.
decompile Decompiles a .jasper file into a .jrxml file.
docx Generates a DOCX version of the report by converting the .jrprint file.
fill Fills the compiled report design with data and produces the .jrprint file.
html Generates an HTML version of the report by converting the .jrprint file.
javac Compiles the java source files of the test application.
ods Generates an ODS version of the report by converting the .jrprint file.
odt Generates an ODT version of the report by converting the .jrprint file.
pdf Generates a PDF version of the report by converting the .jrprint file.
pptx Generates a PPTX version of the report by converting the .jrprint file.
print Sends the report in the .jrprint file to the printer.
rtf Generates an RTF version of the report by converting the .jrprint file.
test Runs the sample.
view Launches the report viewer to preview the report stored in the .jrprint file.
viewDesign Launches the design viewer to preview the compiled report design.
viewDesignXml Launches the design viewer to preview the XML report design.
viewXml Launches the report viewer to preview the generated report stored in an XML file.
writeApi Generates a .java version of the report by converting the .jrxml file.
writeApiXml Generates a .jrxml version of the report by executing the API version of the report design.
xls Generates an XLS version of the report by converting the .jrprint file using the POI library.
xlsx Generates an XLSX version of the report by converting the .jrprint file.
xml Generates an XML version of the report by converting the .jrprint file.
xmlEmbed Generates an XML version of the report by converting the .jrprint file.
Default target: test
Running ant test unfortunately gives:
Buildfile: /home/howard/Downloads/Jaspersoft/Jasper Reports/jasperreports-6.6.0/demo/samples/scriptlet/build.xml
test:
[echo] ===================================
[echo] Scriptlet Sample
[echo] ===================================clean:
clean-sample:
javac:
prepare-compile-classpath:
compile:
BUILD FAILED
/home/howard/Downloads/Jaspersoft/Jasper Reports/jasperreports-6.6.0/demo/samples/scriptlet/build.xml:111: The following error occurred while executing this line:
/home/howard/Downloads/Jaspersoft/Jasper Reports/jasperreports-6.6.0/demo/samples/scriptlet/build.xml:11: The following error occurred while executing this line:
/home/howard/Downloads/Jaspersoft/Jasper Reports/jasperreports-6.6.0/demo/samples/build.xml:40: The following error occurred while executing this line:
/home/howard/Downloads/Jaspersoft/Jasper Reports/jasperreports-6.6.0/build.xml:14: Problem: failed to create task or type antlib:org.apache.ivy.ant:configure
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
No types or tasks have been defined in this namespace yetThis appears to be an antlib declaration.
Action: Check that the implementing library exists in one of:
-/usr/share/ant/lib
-/home/howard/.ant/lib
-a directory added on the command line with the -lib argument
Total time: 0 seconds
ant clean gives:
Buildfile: /home/howard/Downloads/Jaspersoft/Jasper Reports/jasperreports-6.6.0/demo/samples/scriptlet/build.xml
clean:
clean-sample:
BUILD SUCCESSFUL
Total time: 0 seconds
ant compile also fails:
Buildfile: /home/howard/Downloads/Jaspersoft/Jasper Reports/jasperreports-6.6.0/demo/samples/scriptlet/build.xml
compile:
prepare-compile-classpath:
compile:
BUILD FAILED
/home/howard/Downloads/Jaspersoft/Jasper Reports/jasperreports-6.6.0/demo/samples/scriptlet/build.xml:15: The following error occurred while executing this line:
/home/howard/Downloads/Jaspersoft/Jasper Reports/jasperreports-6.6.0/demo/samples/build.xml:40: The following error occurred while executing this line:
/home/howard/Downloads/Jaspersoft/Jasper Reports/jasperreports-6.6.0/build.xml:14: Problem: failed to create task or type antlib:org.apache.ivy.ant:configure
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
No types or tasks have been defined in this namespace yetThis appears to be an antlib declaration.
Action: Check that the implementing library exists in one of:
-/usr/share/ant/lib
-/home/howard/.ant/lib
-a directory added on the command line with the -lib argument
Total time: 0 seconds
Not sure which name they mean?
So they say to check, and for me in /usr/share/ant/lib I'm seeing the following files:
ant-antlr.jar
ant-apache-bcel.jar
ant-apache-bsf.jar
ant-apache-log4j.jar
ant-apache-oro.jar
ant-apache-regexp.jar
ant-apache-resolver.jar
ant-apache-xalan2.jar
ant-commons-logging.jar
ant-commons-net.jar
ant.jar
ant-javamail.jar
ant-jdepend.jar
ant-jmf.jar
ant-jsch.jar
ant-junit4.jar
ant-junit.jar
ant-launcher.jar
ant-swing.jar
ant-testutil.jar
jsch.jar
junit.jar
Is this what they mean by "the implementing library"?
.. I'm lost and don't know where to go from here... ???
From Jaspersoft web site, in: Home » Exchange » JasperReports® Library download jasperreports-6.6.0-project.tar.gz. Inside it under demo/samples/scriptlet are docs/ reports/ and src/ folders.