JasperReports for Java Developers
上QQ阅读APP看书,第一时间看更新

Chapter 2. Adding Reporting Capabilities to Java Applications

We can easily add reporting capabilities to Java applications by taking advantage of the classes included in the JasperReports class library. JasperReports can be easily embedded into both client and server-side Java applications, simply by adding the required libraries to our CLASSPATH and calling the appropriate methods in the JasperReports API.

By the end of this chapter, we will be able to:

  • Identify the purpose of the several downloads that can be found at the JasperReports website
  • Identify required libraries for adding reporting capabilities to Java applications
  • Identify optional libraries that can be used to enhance the reporting capabilities of Java applications
  • Set up our development and execution environment to successfully add reporting capabilities to Java applications

Downloading JasperReports

JasperReports can be downloaded from http://www.sourceforge.net/projects/jasperreports. When visiting the JasperReports download page, you should see an image similar to the following around the middle of the page:

Downloading JasperReports

JasperReports can be downloaded by clicking on this image. Once you click on the image, you should see a window similar to the following on your browser:

Downloading JasperReports

Clicking on the JasperReports 1.2.2 link at the top left cell should take you to a page containing a tree displaying current and previous versions of JasperReports.

Each node in the tree contains three download links. It is not always clear what exactly is downloaded by clicking on these links. For this reason, a brief explanation for each link is given next.

The first link is to download a JAR file containing a subset of the JasperReports functionality. Specifically, it contains classes that can be used to display jrprint files, which are reports in JasperReports' native format. This file is offered as a separate download as a convenience for developers. It can be used for applications or applets that do not require full reporting capabilities, but which need to display generated reports. The file name has a suffix of applet. However, there is nothing preventing us from using it with standalone applications. This file is approximately 187 KB in size.

The second link is the complete JasperReports class library. It contains all the classes necessary to compile, fill, and export reports, but does not include any additional libraries JasperReports depends upon. This is the minimum file we need to add full reporting capabilities to our Java applications. This file is approximately 1.3 MB in size. If we choose to download this file, then we need to download JasperReports dependencies separately. These dependencies are listed in the following table:

The third and the last link is a ZIP file containing the complete JasperReports class library along with all the required and optional libraries. The ZIP file also includes the JasperReports source code, as well as a lot of source code providing examples demonstrating JasperReports' functionality. This file is approximately 24 MB in size. Unless internet connectivity speed is an issue, downloading this file is recommended since it includes everything we need to create reports with JasperReports. Moreover, the included examples are a great way to learn how to implement the different JasperReports features. All of the examples in the file come with an ANT build file containing targets to compile and execute. We will refer to this file as the JasperReports project file, or, more succinctly, as the project ZIP file.

Note

Detailed instructions on installing ANT can be found in the online ANT manual at http://ant.apache.org/manual/index.html.

Once we have downloaded the appropriate file for our purpose, we need to set up our environment to be able to start creating reports. In the next section, we discuss how to do this, assuming that the project ZIP file was downloaded.

Setting Up Our Environment

To set up our environment to get ready to start creating reports, we need to extract the JasperReports project ZIP file to a location of our choice. Once we extract the project ZIP file, we should see a jasperreports-1.2.2 directory containing the following files and directories:

Setting Up Our Environment
  • build: A directory containing the compiled JasperReports class files.
  • 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 compiled form.
  • changes.txt: A text document explaining the differences between the current and previous versions of the JasperReports class library.
  • demo: A directory containing various examples demonstrating several aspects of JasperReports functionality.
  • dist: A directory containing a JAR file that contains the JasperReports library. We should add this JAR file to our CLASSPATH to take advantage of JasperReports.
  • docs: A directory that contains a local copy of the JasperReports website.
  • lgpl.txt: A text document that contains the full text of the LGPL license.
  • lib: A directory containing all libraries needed, both to build JasperReports and to use it in our applications.
  • readme.txt: A text document containing instructions on how to build and execute the supplied examples.
  • src: A directory containing the JasperReports source code.

    Tip

    Getting Up and Running Quickly

    To get up and running quickly, the files to be added to the CLASSPATH are the JasperReports JAR file (its file name should be jasperreports-1.2.2.jar or similar, depending on the exact JasperReports version) which can be found under the dist subdirectory of the directory created while extracting the project ZIP file, or downloaded directly as described above, and all the JAR files under the lib subdirectory of the directory created while extracting the project ZIP file. By adding these files to the CLASSPATH, we don't have to worry about the CLASSPATH when implementing additional functionality (e.g. exporting to PDF or producing charts).