Jump to content
JasperReports Library 7.0 is now available ×

run reports outside of iReports


Recommended Posts

  • 4 years later...
  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

I'm sure there are many people on this board with more experience than me, but here's my answer, for what it's worth.  YES, you can absolutely run reports outside of iReport.  iReport is intended to be a report authoring environment, not the way an actual end user would run the report.  Two common options for deploying your report once it's written are either (a) use the JasperServer plug-in that allows you to copy your jrxml file to the JasperServer repository so that users of JasperServer can now access a copy of your report, or (b) write some Java (or other?) code that will at a minimum establish a connection to your database, load the .jrxml file, compile it into a .jasper file, "fill" the report, export the report to an appropriate format, and then potentially view the report.  Here's a sample of a (non-production) program that does all that and a little bit more.   You need to import several net.sf.jasperreports files for it to compile:


import java.sql.Connection;
import java.sql.DriverManager;
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 net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import java.io.OutputStream;
import java.math.*;
import java.util.*;

public class JasperReportGenerator {
  public JasperReportGenerator() {
  public static Connection connectToDatabase(String databaseName, String userNam
e, String password) {
    Connection connection = null;
    try {
      connection = DriverManager.getConnection(databaseName,userName,password);
    } catch(Exception e) {
      String text = "Could not connect to the database: " + e.getMessage() + " "
 + e.getLocalizedMessage();
    return connection;
  public static void runReport(String databaseName, String userName, String pass
word, String reportFile) throws net.sf.jasperreports.engine.JRException {
//    try {
      Connection connection = connectToDatabase(databaseName, userName, password
System.out.println("Done with connectToDatabase!");
      JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
System.out.println("Done with load!");
      JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesig
System.out.println("Done with compileReport!");
      JRParameter[] jrParameters = jasperReport.getParameters();
      for (JRParameter param : jrParameters) {
System.out.println("Parameter : " + param.getName());
System.out.println("    Class Name: " + param.getValueClassName());
System.out.println("    isSystemDefined: " + param.isSystemDefined());
      HashMap jasperParameter = new HashMap();
      String customTitle = new String("Title from Java Program");
      BigDecimal cutoffAmt = new BigDecimal("5000.00");
      JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, jaspe
rParameter, connection);
System.out.println("Done with fillReport!");
      JRPdfExporter export = new JRPdfExporter();
System.out.println("Done with setJasperPrint!");
System.out.println("Done with exportReport!");
//      JasperViewer.viewReport(jasperPrint);
// System.out.println("Done with viewReport!");
//   } catch(Exception e) {
//     String text = "Could not create the report " + e.getMessage() + " " + e.g
//      System.out.println(text);
//    }

Post Edited by cbarlow3 at 12/16/2010 16:19
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...