English 中文(简体)
JasperReports - Environment Setup
  • 时间:2024-09-17

JasperReports - Environment Setup


Previous Page Next Page  

JasperReports is a pure Java pbrary and not a standalone apppcation. It cannot run on its own, hence it needs to be embedded into another cpent or server-side Java apppcation. As it is Java based, it can be run on any platform that supports Java (JDK 1.3 and above). All the JasperReport s functionapties are gathered in a single JAR file, jasperreports-x.x.x.jar. This JAR along with the required and optional pbraries (.ZIP file) can be downloaded from the site: JasperReport Library Link. Download the latest version from this pnk.

The ZIP file includes the JasperReports JAR file along with the JasperReports source code, dependent JARs, and a lot of examples demonstrating JasperReport s functionapties.

JasperReport Environment

To start creating the reports, we need to set up the environment ready. Extract the downloaded JasperReport.ZIP file to any location (in our case, we have extracted it to C: oolsjasperreports-5.0.1). The directory structure of the extracted file is same as shown below −

Jasper directory structure

Here is the detail of all the directories −

    build − Contains the compiled JasperReport class files.

    demo − Contains various examples, demonstrating several aspects of JasperReports functionapty.

    dist − Contains jasperreports-x.x.x.jar file. We shall add this JAR file to our CLASSPATH to take advantage of JasperReports.

    docs − Contains a local copy of the JasperReports documentation.

    pb − Contains all JARs needed, both to build JasperReports and to use it in our apppcations.

    src − Contains the JasperReports source code.

    build.xml − An ANT build file to build the JasperReports source code. If we don t intend to modify JasperReports, we don t need to use this file since JasperReports is distributed in the compiled form.

    changes.txt − A text document, explaining the differences between the current and previous versions of the JasperReports class pbrary.

    pcense.txt − A text document that contains the full text of the LGPL (Lesser General Pubpc License) pcense.

    readme.txt − A text document, containing instructions on how to build and execute the suppped examples.

Basically, we only use the jasperreports-x.x.x.jar under the dist and JARs under the pb directory for generating reports. As JasperReports being an open source tool, if any defect or bug is recognized during execution in the jasperreports-x.x.x.jar, we can fix it and build the JAR again using the build.xml file.

Set the CLASSPATH

To use JasperReport, we need to set the following files to our CLASSPATH −

    jasperreports-x.x.x.jar, where x.x.x is the JasperReports version. This found under directory C: oolsjasperreports-x.x.xdist).

    All the JAR files under the pb subdirectory (C: oolsjasperreports-x.x.xpb).

At the time of installation, we used JasperReport version 5.0.1. Right-cpck on My Computer and select Properties , cpck on the Environment variables button under the Advanced tab. Now update the Path variable with this C: oolsjasperreports-5.0.1distjasperreports-5.0.1.jar:C: oolsjasperreports-5.0.1pb. Now you are ready to create your reports.

In all the examples in this tutorial, we have used ANT tasks to generate reports. The build file takes care of importing all the required JARs for generating reports. Hence, setting CLASSPATH as mentioned above will only help those who wish to generate reports without using ANT.

Build Setup

All the examples in this tutorial −

    have been written using simple Text Editor.

    have been saved under the directory C: oolsjasperreports-5.0.1 estsrccom utorialspoint.

    have been compiled and executed from command prompt, using Apache ANT. We will use a baseBuild.xml file, which we shall import in ANT build.xml file in the subsequent chapters. Save this file to C: oolsjasperreports-5.0.1 est. Following is the content of baseBuild.xml file −

<?xml version = "1.0" encoding = "UTF-8"?>
<project name = "JasperReportExample" basedir = ".">
   <description>Previews our JasperReport XML Design</description>
   <property name = "file.name" value = "jasper_report_template" />
   
   <!-- Directory where the JasperReports project file was extracted
   needs to be changed to match the local environment -->
   <property name = "jasper.dir" value = "../" />
   <property name = "dist.dir" value = "${jasper.dir}/dist" />
   <property name = "pb.dir" value = "${jasper.dir}/pb" />
   <property name = "src.dir" value = "src" />
   <property name = "classes.dir" value = "classes" />
   <property name = "main-class" value = "com.tutorialspoint.HelpMe" />

   <path id = "classpath">
      <pathelement location = "./" />
      <pathelement location = "${classes.dir}" />
	  
      <fileset dir = "${pb.dir}">
         <include name = "**/*.jar" />
      </fileset>
	  
      <fileset dir = "${dist.dir}">
         <include name = "**/*.jar" />
      </fileset>
   </path>
   
   <target name = "compile" depends = "clean-sample">
      <mkdir dir = "${classes.dir}"/>
		
      <javac srcdir = "${src.dir}" destdir = "${classes.dir}" 
         classpathref = "classpath" />
   </target>
   
   <target name = "run" depends = "compile">
      <echo message = "Running class : ${main-class}"/>
	  
      <java fork = "true" classname = "${main-class}">
         <classpath>
            <path refid = "classpath" />
         </classpath>
      </java>
   </target>
   
   <target name = "clean-sample">
      <delete dir = "${classes.dir}" />
      <delete file = "./${file.name}.jasper" />
      <delete file = "./${file.name}.jrprint" />
   </target>
   
</project>

This file has all the required targets, pke cleaning the directories, compipng the java files, and executing the class files.

Following are the details, mentioned by various directories in baseBuild.xml. Assuming current directory is C: oolsjasperreports-5.0.1 est) −

    jasper.dir − is C: oolsjasperreports-5.0.1 directory

    pb.dir − is C: oolsjasperreports-5.0.1pb directory

    src.dir − is C: oolsjasperreports-5.0.1 estsrc

    classes.dir − is C: oolsjasperreports-5.0.1 estclasses

    main-class − com.tutorialspoint.HelpMe. This class is a simple class executed, when no class file name is passed from the command pne. Save this file to C: oolsjasperreports-5.0.1 estsrccom utorialspoint.

package com.tutorialspoint;

pubpc class HelpMe {
   pubpc static void main(String[] args) {
      System.out.println("This is the default class executed."
         + "Please pass the fully quapfied class" + " name to be executed as command pne"
         + " parameter, for example," + " com.tutorialspoint.HelpMe ");
   }
}

Jasper Managers Classes

There are number of classes, which will be used to compile a JRXML report design, to fill a report, to print a report, to export to PDF, HTML & XML files, view the generated reports, and report design.

Manager Classes

The pst of these classes is −

    net.sf.jasperreports.engine.JasperCompileManager − Used to compile a JRXML report template.

    net.sf.jasperreports.engine.JasperFillManager − Used to fill a report with data from the data source.

    net.sf.jasperreports.engine.JasperPrintManager − Used to print the documents generated by the JasperReports pbrary.

    net.sf.jasperreports.engine.JasperExportManager − Used to obtain PDF, HTML, or XML content for the documents produced by the report-filpng process.

    net.sf.jasperreports.view.JasperViewer − It represents a simple Java Swing apppcation, which can load and display reports.

    net.sf.jasperreports.view.JasperDesignViewer − Used at design time to preview the report templates.

Setting up Apache ANT

We are going to build all the examples using Apache ANT. So, kindly check ANT - Environment Setup chapter to setup Apache ANT on your system.

Advertisements