-1

I am trying to call my report and show it but it did not work!!!

this is the code:

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */

package Frames;

import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.util.HashMap;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.swing.*;

/**
 *
 * @author Ma3d Sa3eed
 */
public class ReportViewer extends javax.swing.JFrame {

    java.awt.Container c;

    public ReportViewer(String rptPath, HashMap parameter) {
        try {
            Class.forName("oracle.jdbc.OracleDriver");
            java.sql.Connection con = java.sql.DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ora10g", "employees", "manager");
            JasperPrint print = JasperFillManager.fillReport(rptPath, parameter, con);
            JRViewer viewer = new JRViewer(print);
            c = getContentPane();
            c.setLayout(new java.awt.BorderLayout());
            c.add(viewer);
            show();
        } catch (Exception x) {
            x.printStackTrace();
        }
    }
}

2 Answers2

0

I would load a JasperReport object and pass the fill manager that and not just a path like so with your rptPath (assuming it's the full file path of your jasper report):

JasperReport report = (JasperReport) JRLoader.loadObject(rptPath);
JasperPrint print = JasperFillManager.fillReport(report, parameter, con);

Then pass that to the JFrame you want to show the report in.

John
  • 376
  • 1
  • 7
  • Thanks for your help, brother. I am still have an exception, this is my call section: public static void main(String...args){ new ReportViewer("C:\\Users\\Ma3d Sa3eed\\Documents\\NetBeansProjects\\Employees\\src\\Reports\\report1.jrxml", null); } } THIS IS THE StackTrace: run: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory at net.sf.jasperreports.engine.util.JRLoader.(JRLoader.java:68) – Ma'd Sa'eed May 04 '16 at 19:05
  • The error is telling you that you are missing a library used by the JRLoader. I believe it's the commons logging jar from Apache found here: http://commons.apache.org/proper/commons-logging/ – John Jun 14 '16 at 17:26
0

Try this, assuming your abc.jrxml is inside a folder xyz :

InputStream stream = getClass().getClassLoader().getResourceAsStream("/xyz/abc.jrxml");
JasperReport report = JasperCompileManager.compileReport(stream);
JasperPrint finalReport = JasperFillManager.fillReport(report, parameter, con);
PeaceIsPearl
  • 329
  • 2
  • 6
  • 19
  • yes, I have my jrxml file in folder calll Reports and I have changed it to: InputStream stream = getClass().getClassLoader().getResourceAsStream("/Reports/report1.jrxml"); but I am still having an exception called: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory In the Line: JasperReport report = JasperCompileManager.compileReport(stream); How can I fix it? – Ma'd Sa'eed May 05 '16 at 20:38
  • Do you have apache commons jar in your project.? – PeaceIsPearl May 05 '16 at 20:48