Java Reference
In-Depth Information
The following code fragment demonstrates the use of the
JasperCompileManager.
compileReportToFile()
method:
package net.ensode.jasperbook;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
public class FirstReportCompile
{
public static void main(String[] args)
{
try
{
System.out.println("Compiling report...");
JasperCompileManager.compileReportToFile(
"reports/FirstReport.jrxml");
System.out.println("Done!");
}
catch (JRException e)
{
e.printStackTrace();
}
}
}
After compiling and executing the code, we should see a file called
FirstReport.
jasper
in the file system. This file is the compiled template in JasperReports'
native format. For this example, we chose to use the first version of the
JasperCompileManager.compileReportToFile()
method we discussed, since by
default the root file name is used for the compiled report, and we did not have an
in-memory representation of the JRXML template. If we had wished to use a
different root file name for the compiled report template, we should have used the
second version of the
JasperCompileManager.compileReportToFile()
method
and specified the desired file name as the second parameter.
Previewing the Compiled Report Template
The
net.sf.jasperreports.view.JasperDesignViewer
discussed previously can
be used to preview compiled report templates as well as JRXML templates. Again the
easiest way to execute this utility is to wrap a call to it into an ANT target. Let us add
a second ANT target to our
build.xml
file. We will call this new target
viewDesign
,
as it will let us preview the compiled report.