Skip to content

A Simple Program

In conclusion, the following is an example of a simple program that shows how to produce a PDF file from a Jasper file using a data source named JREmptyDataSource, a utility data source that provides zero or more records without fields. The file test.jasper, referenced in the example, is the compiled version of the code in A simple JRMXL file example.

JasperTest.java

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.*;
import java.util.*;
public class JasperTest
{
    public static void main(String[] args)
    {
        String fileName = "/devel/examples/test.jasper";
        String outFileName = "/devel/examples/test.pdf";
        HashMap hm = new HashMap();
        try
        {
        JasperPrint print = JasperFillManager.fillReport(
        fileName,
        hm,
        new JREmptyDataSource());
        JRExporter exporter = 
        new net.sf.jasperreports.engine.export.JRPdfExporter();
           exporter.setParameter(
                JRExporterParameter.OUTPUT_FILE_NAME,
                outFileName);
            exporter.setParameter(
            JRExporterParameter.JASPER_PRINT,print);
            exporter.exportReport();
            System.out.println("Created file: " + outFileName);
        }
        catch (JRException e)
        {
            e.printStackTrace();
            System.exit(1);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.exit(1);
        }
    }
}