Step by Step Instructions on How to Run a Jasper Report from Java

Posted: April 11, 2008 in HowTo, Jasper Report, Java, Tips, Web reporting, WebProgramming
Tags: , , , , ,

Nguồn: http://www.cise.ufl.edu/~otopsaka/CIS4301/ReportDemo/ReportFromJava.html

Oguzhan Topsakal

March 2006

This mini-tutorial shows you how to run a report (Jasper Report) from Java code. To run a Jasper Report from Java code you need the following Jasper Report libraries (jar files):

  • jasperreports-1.2.0.jar,
  • commons-beanutils-1.5.jar,
  • commons-collections-2.1.jar,
  • commons-digester-1.7.jar,
  • commons-logging-1.0.2.jar.

You can find these libraries inside the lib directory of the iReport-1.2.0 installation. (For instance, if you have installed iReport in the C:\Courses\CIS4301\ReportGenerator\iReport-1.2.0 directory, you can find these libraries in C:\Courses\CIS4301\ReportGenerator\iReport-1.2.0\lib directory. For information on how to install iReport check this page.)

You also need the oracle JDBC library (classes12.jar file) to connect to the database. You need to put these files into your Java CLASSPATH.

The following Java code uses a Jasper Report design (reportFile paramater) and fills the report with data from database and views the report. An example of a Jasper Report is given here.

public static void runReport(String databaseName, String userName, String password,String reportFile) {
try{
JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
Connection jdbcConnection = connectDB(databaseName, userName, password);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, jdbcConnection);
JasperViewer.viewReport(jasperPrint);
}catch(Exception ex) {
String connectMsg = “Could not create the report ” + ex.getMessage() + ” ” + ex.getLocalizedMessage();
System.out.println(connectMsg);
}
}

The complete Java Class code is as the following: ReportDriver.java

You can use ReportDriver.java as a driver program from other Java classes as well. Here is an example.

import java.sql.*;
import net.sf.jasperreports.view.JasperViewer;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.JasperReport;
import java.io.OutputStream;

/**
* Driver program to connect to a database and to view a jasper report (.jrxml)
* @author Oguzhan Topsakal
* @since 23 March 2006
*
* Required jar files to run this class:
* 1. jasperreports-1.2.0.jar
* 2. classes12.jar (for Oracle JDBC connection)
* 3. commons-beanutils-1.5.jar
* 4. commons-collections-2.1.jar
* 5. commons-digester-1.7.jar
* 6. commons-logging-1.0.2.jar
*
*/

public class ReportDriver {

/**
* Constructor for ReportDriver
*/
public ReportDriver() {
}

/**
* Takes 3 parameters: databaseName, userName, password
* and connects to the database.
* @param databaseName holds database name,
* @param userName holds user name
* @param password holds password to connect the database,
* @return Returns the JDBC connection to the database
*/
public static Connection connectDB(String databaseName, String userName, String password) {
Connection jdbcConnection = null;
try{
Class.forName(“oracle.jdbc.driver.OracleDriver”);
jdbcConnection = DriverManager.getConnection(databaseName,userName,password);
}catch(Exception ex) {
String connectMsg = “Could not connect to the database: ” + ex.getMessage() + ” ” + ex.getLocalizedMessage();
System.out.println(connectMsg);
}
return jdbcConnection;
}

/**
* Takes 4 parameters: databaseName, userName, password, reportFileLocation
* and connects to the database and prepares and views the report.
* @param databaseName holds database name,
* @param userName holds user name
* @param password holds password to connect the database,
* @param reportFile holds the location of the Jasper Report file (.jrxml)
*/
public static void runReport(String databaseName, String userName, String password,String reportFile) {
try{
JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
Connection jdbcConnection = connectDB(databaseName, userName, password);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, jdbcConnection);
JasperViewer.viewReport(jasperPrint);
}catch(Exception ex) {
String connectMsg = “Could not create the report ” + ex.getMessage() + ” ” + ex.getLocalizedMessage();
System.out.println(connectMsg);
}
}

/**
* Uses runReport method to connect to the database and to prepare and view the report.
* @param args Takes 4 arguments as an input: databaseName, userName, password, reportFileLocation
* args[0] holds database name,
* args[1] holds user name
* args[2] holds password to connect the database,
* args[3] holds the location of the Jasper Report file (.jrxml)
*/
public static void main(String[] args) {
if (args.length == 4) {
String databaseName = args[0] ;
String userName = args[1];
String password = args[2];
String reportFile = args[3];
runReport(databaseName, userName, password, reportFile);
}else{
System.out.println(“Usage:”);
System.out.println(“java ReportDriver databaseName userName password reportFileLocation”);
}
return;
}
}

A batch file that compiles the above Java Class is like the following (Please note that all the library classes are placed inside the javaLib directory under the current working directory) :

javac -classpath .;javaLib\classes12.jar;javaLib\jasperreports-1.2.0.jar ReportDriver.java
pause

A batch file that runs the ReportDriver Java class is like the following (Again, please note that all the library classes are placed inside the javaLib directory under the current working directory):

REM first parameter is your Oracle connection string. For instance, jdbc:oracle:thin:@oracle1.cise.ufl.edu:1521:orcl
REM second parameter is your Oracle database user name
REM third parameter is database password
REM fourth parameter is report file location. For instance, C:\Courses\CIS4301\WEBPAGE\ReportDemo\jrTemplate\YourUFID_1.jrxml
java -cp .;javaLib\classes12.jar;javaLib\jasperreports-1.2.0.jar;javaLib\commons-digester-1.7.jar;javaLib\commons-collections-2.1.jar;javaLib\commons-logging-1.0.2.jar;javaLib\commons-beanutils-1.5.jar ReportDriver %1 %2 %3 %4
pause

Here is an image showing how I run the above batch file:

This is the report I get after running my Jasper Report template:

For Jasper Report API: http://jasperreports.sourceforge.net/api/index.html

For commons library API: http://jakarta.apache.org/commons/

Prepared by Oguzhan Topsakal

March, 2006

site statistics
About these ads
Comments
  1. rajib says:

    thanks a lot, this is a very useful example

  2. Ranjan Prasad says:

    Please if u have Solution the send me the Code….

    Hi i am Using jasper report I develop a project with struts 1.2 Frame work ; My project generate a PDF report based on a specified database.

    The problem is it open the PDF file on the Server Terminal(side) i.e (http://localhost:8084/GeoTrak/getdetailReportPage.do) but not on the client Terminal(side) i.e (http://192.168.100.35:8084/GeoTrak/getdetailReportPage.do). i.e when client call jasperviewer then application open jasperreport in server Terminal(side) not on the client Terminal(side).

    Please Help Me …..

    Code ……Struts Action class.:……………………………..

    String fullPath=getServlet().getServletContext().getRealPath(“reports/”+pf.getReportList()+”.jasper”);

    JPrint=JasperFillManager.fillReport(fullPath,params,con);

    JasperViewer.viewReport(JPrint,true);

    con.close();

    Thank

    Best regards Ranjan

  3. Jigar Naik says:

    Thanks a lot.. nice example…

  4. prabhu says:

    hi

    i have created a report using jasper viewer in netbeans. while i’m running in the nebeans, it shows the report. but when i’m trying run from a jar file it is not working

    help me…..

    thanks in advance……….

  5. NITESH PAWAR says:

    Hi I wanted to see the code of .jrxml file of example given above. My column headers are not being displayed.

  6. Sneha says:

    I am New to jaspersoft…can anyone explain me the procedure

  7. Sneha says:

    I cant understand anything in it

  8. tom1953 says:

    Hi. I have been using your example for some years now and it has proved – wonderful
    Now I have set a paramerter $P{User Name} in iReport, Which asks for a username at runtime
    this works in iReport and JasperServer but will not show in the Viewier.
    is there any library I need for this.
    thanks

  9. Riya says:

    maja na avi bhai … kaik navin ke..

  10. prema says:

    Its really useful and awesome.. being an beginner it supports me a lot to generate the report.
    Thank you so much

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s