The JasperReports Ultimate Guide
Page 12
When compiling Java source files, the most important thing seems to be the classpath. If the Java
compiler does not find in the supplied classpath all the classes that are referenced by the source files it
tries to compile, the whole process fails and stops, the errors being displayed on the console.
The same happens also when JasperReports compiles the report expressions class. It is important then
to make sure we supply to the Java compiler at runtime the correct classpath for the compilation
process to succeed. For instance, we have to make sure that in the classpath we supply the custom
classes that we might use in the report expressions.
There is a default behavior for this aspect also. If no special classpath for compiling the report classes
is supplied, the engine will use the current JVM classpath returned by the system property
java.class.path. This default behavior can be overridden by putting the desired classpath in the
system property called
jasper.reports.compile.class.path.
You can see the jsp/compile.jsp and WEB-INF/classes/servlets/CompileServlet.java
files in the webapp sample provided, for the code snippet that makes use of this Java system properties
to override the default behavior of the JasperReports report compilation process.
Most of the time, compiling a report requires only a simple call to the JasperReports library like in the
following line of code:
dori.jasper.engine.JasperCompileManager.compileReport(myXmlFileName);
This call will produce the compiled report design and will store it in a file with the .jasper extension,
located in the same directory as the supplied XML report design file.
3.3 Report Design Preview
The JasperReports library is not shipped with an advanced GUI tool to help the design work. At this
time, there are at least 4 projects that try to provide such a tool.
However, the library contains a very helpful visual component that allows report creators to preview
the report designs as they build them.
The dori.jasper.view.JasperDesigner class is a simple Swing based Java application that can
load and display a report design either in its XML form or in the compiled form. Even if it is not a
complex GUI application, lacking advance functionality like the drag and dropping of the visual report
elements, it is very helpful instrument. All the supplied samples were created using this design viewer.
All the supplied samples have already prepared ANT tasks in their build.xml files that will launch
this design viewer for you to see the report designs.
In fact there are 2 ANT tasks for each sample report: viewDesign and viewDesignXML.
The first one loads the compiled report design that is normally found in the .jasper file. The second
one loads the XML report design, being the most useful since you can edit the XML file and push the
Reload button to immediately see the modification appearing on the screen.
If you have the ANT build tool installed on your system, in order to preview a sample report design,
you simply go to the desired sample directory and launch from the command line something like this:
>ant viewDesignXML
or
>ant viewDesign
In case you do not have ANT installed and you don't want to get it (you'll miss all the fun), here's the
complete command line that will launch the design viewer to preview one of the sample XML report
designs. We assume that you have a copy of the entire JasperReports source tree on your system
(Windows command line syntax):