How to call or invoke iReport from Java Application

howto 19 Comments
  • Sharebar

This is just a short tutorial on how to code / call / invoke iReport from Java Application using netbeans, i created this tutorial for my Java class and i would like to share this with everyone. i hope you will understand the short video clip i created for this one.

Here is the link where you can download the required jar files for your project.

I recommend you watch this video in fullscreen and in HD..

Here is the source code :

[code language="java"]

/**
*
* @author joeLL
*/
import java.sql.*;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.swing.JRViewer;
import javax.swing.*;

public class ReportView extends JFrame
{
Connection conn = null;

void showReport()

{ try
{
//database connection here
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn =DriverManager.getConnection( "jdbc:odbc:joell" );

JasperReport report = JasperCompileManager.compileReport("report1.jrxml");
JasperPrint print = JasperFillManager.fillReport(report,null, conn);
JRViewer viewer = new JRViewer(print);
viewer.setOpaque(true);
viewer.setVisible(true);
//make your JFrame visible
this.add(viewer);
this.setSize(300,200);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);

}
catch(Exception ex)
{
System.out.println("CAUSE: " + ex.getCause());
System.out.println("MESSAGE" + ex.getMessage());
System.out.println("LOCAL MESSAGE" + ex.getLocalizedMessage());
ex.printStackTrace();
}
}

public static void main(String args[])
{
new ReportView().showReport();
}

}
[/code]

you may place your comments or questions below and i will try my best to answer those questions.

Related Posts Plugin for WordPress, Blogger...

Incoming search terms:

  • Raphael

    JasperReport <span class="IL_AD" id="IL_AD5">report</span> = JasperCompileManager.compileReport("report1.jrxml");

    uhm sir sa code po na ito, anu po ibig sabihin nung span class="IL_AD" …..

    and san po galing ung "report1.jrxml" na file?

    • http://www.jlapitan.com jlapitan

      span class? wla nmn ganyan sa code… at un report1.jrxml, if hindi mo alam it means absent ka nung itinuro ko un.

      • Paulo Castro

        sir .. sa webinar nyo po ata itinuro yan ..

        wala ako nun .. wala kami internet nung time na nagturo kau ng JReport .. xD

        Paulo Castro, section 3CS ..

        • http://www.jlapitan.com jlapitan

          walang webinar n ngyari..

          • Paulo

            aw .. ganun po ba sir? ..

            waaa .. hindi ko alam kung san galing ung "report1.jrxml" .. sir..san po galing un? .. hehe .. =)

      • raphael

        sir pwde po paturo ulit kung paano magcreate ng jrxml file?

  • Jairus

    ser….may error….
    :
    CAUSE: java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot find the input table or query 'Students'. Make sure it exists and that its name is spelled correctly.

    MESSAGEError executing SQL statement for : report1

    LOCAL MESSAGEError executing SQL statement for : report1

    • Paulo

      it means .. hindi daw po nagEexist dun sa database na 'Students' [if ever na Students talaga ung name nung table] ung data . or null ung table .. [un lang po alam ko pag ganun .. xD]

  • iqbal

    hei , i got some error while using ur script, its says like this
    net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException: report1.jrxml (The system cannot find the file specified)
    CAUSE: java.io.FileNotFoundException: report1.jrxml (The system cannot find the file specified)
    MESSAGEjava.io.FileNotFoundException: report1.jrxml (The system cannot find the file specified)
    LOCAL MESSAGEjava.io.FileNotFoundException: report1.jrxml (The system cannot find the file specified)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:168)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:148)
    at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:146)
    at javaapplication4.Main.displayReport(Main.java:41)
    at javaapplication4.Main.main(Main.java:65)
    Caused by: java.io.FileNotFoundException: report1.jrxml (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:106)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:163)

    i think my application can't access my report file
    here my code
    public class Main extends JFrame{
    Connection con = null;
    Statement st = null;
    String url = "jdbc:mysql://localhost:3306/pegawai";
    String user = "root";
    String pass = "";
    public Main()
    {
    try{
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    con = DriverManager.getConnection(url,user,pass);
    st=con.createStatement();
    }catch(Exception ex){
    ex.printStackTrace();
    }
    }
    public void displayReport() {

    try{

    JasperReport report = JasperCompileManager.compileReport("report1.jrxml");
    JasperPrint Print = JasperFillManager.fillReport(report, null, con);
    JRViewer viewer = new JRViewer(Print);
    viewer.setOpaque(true);
    viewer.setVisible(true);
    this.add(viewer);
    this.setSize(300,200);
    this.setVisible(true);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    }
    catch(Exception ex)
    {
    System.out.println("CAUSE: " + ex.getCause());
    System.out.println("MESSAGE" + ex.getMessage());
    System.out.println("LOCAL MESSAGE" + ex.getLocalizedMessage());
    ex.printStackTrace();
    }
    }

    public static void main(String args[])
    {
    new Main().displayReport();
    }
    }

    i place report1.jrxml in same package as my application in src/myaplication/report1.jrxml

    can u help me , whats wrong with my code

  • Dondell

    why it gives me an error…

    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/digester/Digester
    at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:150)
    at Reports.ReportView.showReport(ReportView.java:32)
    at Reports.ReportView.main(ReportView.java:58)
    Caused by: java.lang.ClassNotFoundException: org.apache.commons.digester.Digester
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    … 3 more

    • shagun

      hey dondell- your commons jar file is missing (for org/apache/commons/digester….)
      for other errors, add all jar files in your library folder

  • sagar

    hello sir can u tell me the steps of how to integrate ireport tool to my netbeans 6.8 .
    m having import problem in my IDE ..
    i tried everything but not getting it …
    replay soon..
    thanks.

  • sagar

    sir i got it …
    thanks your jar file help me ..
    thank u for helping me out..
    sir if any kind of work is there then tell me ….
    i hv done ma BSC in Computer science …

  • melzaiady

    thank you but i have error
    CAUSE: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
    MESSAGEcom.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
    LOCAL MESSAGEcom.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence

  • Alain

    thank you very much!

  • Sherwin Medina

    hi!!!!! active pa ba ang account na to??? gusto ko sanang matuto……
    urgent kasi..hehe……… nag thesis writing kasi ako at gawa ako ng program pra sa PNP…… reports(print outs) na
    lang ang kulang….

    • http://www.jlapitan.com jlapitan

      ano po need nila?

  • mr naratip

    it's possible to give me the code project of this lesson it would be kind.

  • Imby

    Thanks for the article, please check this article too
    Call Jasper Report / Ireport From Java Application