Thursday, May 1, 2008

iReport Tutorial (part 3) - Calling Report From Java Application

In the previous tutorial, you've learnt how to create a report with iReport. You must be realized that the report can only be executed from iReport. But, in the real case that report should be able to be executed from the other application (usually Java application). How we do this?

Setting CLASSPATH

The first thing you have to do is set your CLASSPATH environment variable. Why? Because Java needs to load some libraries which are needed to execute that report. Those libraries are:

  • jasperreports-2.0.4.jar
  • commons-digester-1.7.jar
  • commons-logging-1.0.2.jar
  • commons-collections-2.1.jar
  • mysql-connector-java-3.1.11-bin.jar

If you are a Linux user, you can set your CLASSPATH by typing this command from your Linux console (I assume your iReport installation directory is /usr/local).

export "CLASSPATH=.:/usr/local/iReport-2.0.4/lib/jasperreports-2.0.4.jar:\
 /usr/local/iReport-2.0.4/lib/commons-digester-1.7.jar:\
 /usr/local/iReport-2.0.4/lib/commons-logging-1.0.2.jar:\
 /usr/local/iReport-2.0.4/lib/commons-collections-2.1.jar:\
 /usr/local/iReport-2.0.4/lib/mysql-connector-java-3.1.11-bin.jar"

But, if you are a Windows user, you can use set CLASSPATH=[your class path] from the command prompt utility.

Write Your Code

Let's move on! Next step is writting your Java code. This code will call the report that we've made in the previous tutorial. To make it easier, I'll give you all the code so you can copy-paste and modify it if necessary. I named this file viewReport.java, but you can use another name that you prefer.



Execute It!

Final step! Compile your Java file by typing javac viewReport.java. After that, execute it with command java viewReport. Before that, make sure your MySQL has started. And, after your program run, you can click on SHOW REPORT button to view the report.

4 comments:

Raul Tobon said...

Thanks you helped me a lot.
Saludos desde Puebla Mexico

Anonymous said...

net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException: /report1.jasper

Dondell said...

It gets an error For me.....


any library i was missing?

sabir said...

Dear Sir,
I Use iReport-4.5.0 and jdk1.6.0_14
when i set classpath then display
error
java.lang.NoClassDefFoundError