Jasper reports

Jasper reports

This time i am describing how make Jasper reports. Lets download iReport which is a tools of Jasper reports. To get more details on Jasper reports and documents please click here. Yet I described on Jasper reports at my another post. Here I am using iReport-3.0.0 zip file. This version i found mysql driver jar found in the lib folder but i did not see the oracle(ojdbc14.jar) driver jar file. If you would like to use oracle please put this jar file into the lib folder.

Jasper Reports step by step

# Create Connection:  Click Data-> Connections/Datasources. Click New button. select Database JDBC Connection -> Next Button ->now put your database information. i gave here one screen shot Jasper reports - dbconnection window

Pressing test button iReport shows the connection status. If you get any error like oracle/mysql class not found, make sure the lib folder contain your database driver jar file. If you get same excepton so go Options -> ClassPath -> Click add jar. now select the related database jar file. iReport supports multiple database so we can produce Jasper reports using different datasource.

# create a report using iReport:  File -> Report Wizard ->

template : none, connection: mysql, SQL Query : Select *from Person.

Next-> select all columns.  Next -> (do not select any group 1 value, if you want to show the report by any group you can select any column.)  Next -> Tabular Layout (ClassicT) Finish.

now you get one report. now you can change the report title. it looks

Jasper reports - mainscreen1Now you can test this Jasper reports by clicking  Build->Complie or Build->Execute(emptyconnection) or Build-> Execute (active connection). after compile the jaspar file will generatewhere the iReport.exe file is located. empty connection will show only report withoutdata butactive connection will show the data.

Since we want to show only first 30 id records, so we have create a parameter variable. this parameter determine how many record we want to show. to create a parameter click View -> Parameter then a window will come, here we can create variable which will only used as variable that scope is the whole report, field that is used for showing a field in the report and finally parameter that takes data from java class.  Click New button ->parameter name: p_id, class type: java.lang.Integer, use as Prompt uncheckd, default Value expression : new Integer(0) that means we assing a default value for this parameter. iReport always consider all the value as Object not primitive thats why i set write as new Integer(0) Paramete Description: paraeter ID . now click OK.

Now lets set the condition in the query. Click Data-> Report Query now put the following line – select *from person where id < $P{p_id}  here $P{p_id} is the parameter. Jasper reports - reportquery in iReport $F { name } is means field and $V{ name } is indicate variable. now click OK.

Now save the report as person.jrxml. if you complie this report you will get person.jasper file. let see the description of person table.

create table person ( id number(6), name varchar(20));

# Execute iReport From Java: I used eclipse to create one java program that execute Jasper reports.  Open eclipse, click New -> New Java Project  Project Name: SimpleiReprot , select Create new project in workspace, select default JRE, select Create separate folders now click Next -> FInish. now create 3 folders ( lib, report-out, report-src) under this project. The project folder structure looks this Jasper reports - folder_arch3

now put the person.jrxml and person.jaspar file into report-src folder. report-out folder will use for the pdf report file. Here I should mention jrxml file is xml formatted file and .jasper file is generated after compiling .jrxml file. Jasper engine using .jasper file to produce Jasper report at runtime. Now copy all the jar files from ireport’s lib folder into your project lib folder.  now refresh the eclipse project.now set the build path for this project. click the right button on the project and select properties and select the Java Build Path from the left side menu and click the Libraries Tab. it looks as

Jasper reports - eclipse project buildpath

Jasper reports – eclipse project buildpath

Now Add Jar button from the right side. Now select SimpleiReprot project and explore the lib folder and select all the jar files of lib folder. now click OK. Select Order and Exprot tab and Select All button from right side. Click OK.

I used 2 java files. DBConnector.java used for creating the database connection and SimpleiReport.java used for execute the Jasper reports.

Jasper reports  are generated by java class

DBConnector class

SimpleiReport Class

If you want to use other database change the DBConnector class.Jasper reports - eclipse-mainClick the right button on the SimpleiReport file and select Run As ->  Java Application. Or you can set Run configaration from the Run->Run Configuration. this run configuration window you have to select the main class. the output console looks

——- output console

Created file: ./report-out/personList.pdf

and personList.pdf file will generate under the report-out folder. the pdf looks as

Jasper reports - output as pdf

Jasper reports – output as pdf

Please ask me if you have any problem or can give me any suggestion.

Thanks

56 thoughts on “Jasper reports

  1. Hi Rajib,
    Ur Blog is mindblowing and gives all the information regarding ireport .. can u please tell me from where can i download iReport plugin for eclipse…

    Please help me regardings this,

    Thanks In advance
    vanishree.B

  2. Hi
    I m working on jasper iReport 3.1.2 nd using Mysql.
    I want to ask how to use ‘ALL’ in parameter.

    As for ex-
    Where (First_Name=$P{F_N} OR ‘ALL’ IN $P{F_N}) in Oracle.

    Plz tell me how to use ‘ALL’ in Mysql connection.

  3. Hi Ritesh Kumar,
    Your problem is not clear for me. please send me details your problem. you can mail me rajib_info @ Yahoo dot com.

  4. hi rajib

    ur report is excellent .. i tried and got a gud result but my prob i need dynamic report for instance iam select from this date to end date. and then my one more doubt is iam using join and inner join table for this also condition would work or not ? give me the solutionss

    thanks in advance
    regards
    karthick

  5. Hi Karthick,

    You can send start,end date by parameter. you can show this type of date using javascript or others. Yes join (outer/inner) is possible. most of time i feed xml data to iReport so that i do not change the jrxmal file later if any change comes.

    Thanks
    Rajib

  6. Hi Rajib,

    I am using i-reporting tool for generating dynamic reports. It’s working fine.
    I want to import the style sheets into the reports. Awaiting your solution.

  7. Hi Rajib,
    i am using IReport To create My Report. Earlier i was using Crystal report where wrting formula is kind of easy task according to me .. any ways here in IReport I am unable to get it right.. can u please tell me how to write formula
    for Example My report has Name of employee, salary , etc
    I want to display page total and the grand total as well , but before the i want to sum up (Total earnings – Total deduction)as my netpayable …
    Hope u understand my query..

    Thanks in advance
    Vanishree

  8. Hi karthick & Vani,
    Yes you can add If.
    Example of if ->
    right click on the field,which you want to add if condition, Select Edit expression and add this line

    $F{gender} == 0?”Male”:”Female”

    but this field must be a string type.

    Example of Add ->

    new Integer( ($F{amount1}+$F(amount2))

    but this field must be a Integer type.
    or you can create a variable for sum the value. you can do that 2 way 1.group by column or 2. create a variable. let me show how you create a variable ,1. click View->variables->New put variable name, type integer if it is sum. calculation type sum, reset type as you want, variable expression -> what field you want to sum. initial value expression -> new Integer(0). here i consider as a int value.

    You can also used wizard. you will get this

    wizard at Expression

    editor, under the Objects and expression tab. when you double click on Addition(+) iReport shows a pop up window which field you wants to add. Same way you can do add, sub, mul and div.
    I guess you can do that your self.

  9. Hi,
    Very well explained.Good tutorial.I have one question, we i want to print array of type user(name,age),iam passing that array as parameter from my java class e.g pram.put(“userArray”,User[]);
    and also i dont have to pass databse connection to jrxml file,my data is in user array and i have to print these values in user array in my jasper report.How can i do that???iam stuck here.
    It will be really appreciated if i could get any help.

    Regards,
    Simer

  10. Hi,
    First of all nice Article.
    I have an issue hope u can resolve it.

    I have developed a Web Application, Reporting is done using Jasper Engine.
    IReport is hitting the Oracle DataBase and fetching Fields, now if i want to use those fields in my Java Program, which is the Servlet of the Web Application. Which Jasper Class Method would help me in getting the Values of those fields. I am able to get the name of the Fields using JasperReport Class. I didnt find any method in Jasper API which may return me a Value of those Fields, It would be really helpfull if someone could help me in this .

    I saw a method in JRFillDataset Class which is returning me Field values, but creating an object of that class require too many Paramters as its Contructor is accepting too many parameters which my program doesn’t support.

    It would be also helpful if there is some method which is returning me an object of type JRFillDataset.

    Thanks in advance.
    Girish Adiga

  11. hi rajib,

    please can you help me with the source files of that tutorial? I wana know how does the result look like to know if this is what I’m looking for.
    thanks in advance

  12. Hi,
    Can you tell me how to declare default value expression for a date parameter? I want the default value of two date parameters to be 22nd of the previous month and 21st of current month. Also, another date parameters to have default values as the first of the current month, first of the year etc..

    Thanks
    Satheesh

    • Hi Satheesh,
      You can make parameter or variable for implementing your object. To do, click view-> variable or parameter.
      click new ->put name,class type, put initial value as ur default value.

  13. Hi Rajib,
    I wanted to know if we fetch the values from database
    then how to set that values in jasper report using java program?is the way is same as u have done it using the param variable .
    e.g. fetch the values in string or int variable and set that in fillreport()function.
    waiting for response.
    Thanks in advance.
    Gauri.

    • HI Gauri,
      I have collected data from database and done some operation(if need) in java class and made xml file that xml data feed to jasper report. you can get this example in my blog.

  14. hi Rajib..
    i am new to iReport,i want to create Gantt chart using iReport,i don’t know what values has to be taken for series,task,subtask expressions while creating the Gantt chart.Iam getting some errors like ‘cannot cast from BigDecimal to String’.

    Thanks in advance.

    Regards.
    Charan

    • I knew you got one exception that is ClassCastException. this error you can solve it by the following line
      if your variable is string then use it in expression ->
      new BigDecimal( stringVariable)

  15. this is the error am getting:
    Error filling print… java.math.BigDecimal
    java.lang.ClassCastException: java.math.BigDecimal      at net.sf.jasperreports.charts.fill.JRFillGanttDataset.customIncrement(JRFillGanttDataset.java:153)      at net.sf.jasperreports.engine.fill.JRFillElementDataset.increment(JRFillElementDataset.java:179)      at net.sf.jasperreports.engine.fill.JRCalculator.calculateVariables(JRCalculator.java:152)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:682)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:255)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:113)      at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:899)      at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:802)      at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:63)      at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:421)      at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:251)      at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:896)      at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:561)      at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:986) 
    Print not filled. Try to use an EmptyDataSource…

  16. hi when i am trying to connect jdbc connection with MYSQL database from PHP application, am getting the following error.If any suggestions/ideas would be appreciated.

    Fatal error: Uncaught [[o:Exception]:
    “java.lang.Exception: CreateInstance failed: new net.sf.jasperreports.engine.JasperCompileManager.
    Cause: java.lang.ClassNotFoundException: net.sf.jasperreports.engine.JasperCompileManager Responsible VM: 1.6.0_11@http://java.sun.com/” at:
    #-16 java.net.URLClassLoader$1.run(Unknown Source)
    #-15 java.security.AccessController.doPrivileged(Native Method)
    #-14 java.net.URLClassLoader.findClass(Unknown Source)
    #0 C:wampbinphpphp5.2.9-2extJava.inc(173): java_ThrowExceptionProxyFactory->getProxy(1, NULL, false)
    #1 C:wampbinphpphp5.2.9-2extJava.inc(379): java_Arg->getResult(false)
    #2 C:wampbinphpphp5.2.9-2extJava.inc(382): java_Client->getWrappedResult(false)
    #3 C:wampbinphpphp5.2.9-2extJava.inc(553): java_Client->getInternalResult()
    #4 C:wampbinphpphp5.2.9-2extJava.inc(1766): java_Client->createObject(‘net.sf.jasperre…’, Array)
    #5 C:wampwwwexamplesrunreport.php(6): Java->Java(‘net.sf.jasperre…’)
    #6 {main}] thrown in C:wampbinphpphp5.2.9-2extJava.inc on line 140

    Thanks in advance.

    Regards.
    Srikanth.

    • Hi Srikanth,

      1. You missing required jar file, jar file is library file in java, in you class path. I never called jasper report from php. please set the required jar file in your class path.
      2. Cause: java.lang.ClassNotFoundException: net.sf.jasperreports.engine.JasperCompileManager Responsible VM: this line indicates you do not have that jar file. I tried for you which is the required jar file but i did not get now. i suggest to you download the ireport.zip(http://jasperforge.org/projects/ireport) and put all the jar file from lib folder to your php class path folder.

      I guess it will help you.

      Thanks
      Rajib

  17. hi Rajib..
    i am new to iReport,
    i got a code from the URL:-> http://www.brandnewreality.com/site/blog/php-and-jasperreports.
    i changed the path of compaileReport() and getconnection().
    But when i compiling that i got an error (i.e Class ‘Java’ not found in C:wampwwwexamplesrunreport.php).
    This is very importent to me to create jasperreports in my company with in the 2 days.please help me.

    Thanks in advice

    Regards.
    Charan

  18. how can i get the java class from this program.i don’t know that java.how can i get ‘java’ class from the PHP application.

  19. Hi Rajib

    When am I integrating jrxml file woth PHP application,I am getting the bugs.its very impertent to me.
    Please check this bugs.I got the code from the URL:->
    http://zeronine.org/lab/index.php/Using_Jasper_report_with_PHP.
    I modified
    $javaBridge = “http://127.0.0.1:8080/”;
    $jasperLib = “C:/Program Files/jasperserver-pro-3.5/scripts/lib”;
    $reportPath = “C:/Documents and Settings/Kalyan/Desktop/reports/”;
    in Testload.php file.But i am getting the following error.please cheak it.

    java stack trace: [[o:ByteArrayOutputStream]:”java.lang.Exception:
    CreateInstance failed: new org.zeronine.japserreports.jdbcConnector.
    Cause: java.lang.ClassNotFoundException: Could not find org.zeronine.japserreports.jdbcConnector in java_require() path.
    Please check the path and the SEL and File permissions. Responsible VM: 1.6.0_11@http://java.sun.com/ at php.java.bridge.DynamicJavaBridgeClassLoader.loadClass(DynamicJavaBridgeClassLoader.java:288) at java.lang.ClassLoader.loadClassInternal(Unknown Source) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at php.java.bridge.SimpleJavaBridgeClassLoader.forName(SimpleJavaBridgeClassLoader.java:206) at php.java.bridge.JavaBridge.CreateObject(JavaBridge.java:453) at php.java.bridge.Request.handleRequest(Request.java:443) at php.java.bridge.Request.handleRequests(Request.java:481) at php.java.bridge.http.ContextRunner.run(ContextRunner.java:140) at php.java.bridge.BaseThreadPool$Delegate.run(BaseThreadPool.java:58)
    Caused by: java.lang.ClassNotFoundException: Could not find org.zeronine.japserreports.jdbcConnector in java_require() path. Please check the path and the SEL and File permissions. … 10 more Caused by: java.lang.ClassNotFoundException:
    Class org.zeronine.japserreports.jdbcConnector not found at php.java.bridge.DynamicClassLoader.loadClass(DynamicClassLoader.java:488) at php.java.bridge.DynamicJavaBridgeClassLoader.loadClass(DynamicJavaBridgeClassLoader.java:286)
    … 9 more “]

    Thanks in Advice

    Regards
    Srikanth.T

  20. hi rajib,

    can you help me please with the source files of that tutorial? Im stil hanging on the problem.
    I wana execute the source file on my java platform to know how does the result look like so that I understand what you’re pointing at through this tutorial.
    thanks in advance

  21. srikanth , can you solve your problem, I got the same or equal problem

    Uncaught [o(Exception):”java.lang.Exception: CreateInstance failed: new net.sf.jasperreports.engine.JasperCompileManager. Cause: java.lang.ClassNotFoundException: Could not find net.sf.jasperreports.engine.JasperCompileManager in java_require() path. Please check the path and the SEL and File permissions. Responsible VM: 1.5.0_10@http://java.sun.com/” at: #-10 php.java.bridge.DynamicJavaBridgeClassLoader.loadClass(DynamicJavaBridgeClassLoader.java:448) #-9 java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319) #-8

    can you help me this drive’s me crazy!!!

  22. please help me , for netbean , desktop application

    Exception in thread “AWT-EventQueue-0” java.lang.NoClassDefFoundError: report name_1244513051714_534644 (wrong name: report32name_1244513051714_534644)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
    at net.sf.jasperreports.engine.util.JRClassLoader.loadClass(JRClassLoader.java:245)
    at net.sf.jasperreports.engine.util.JRClassLoader.loadClassFromBytes(JRClassLoader.java:176)
    at net.sf.jasperreports.engine.design.JRAbstractJavaCompiler.loadEvaluator(JRAbstractJavaCompiler.java:76)
    at net.sf.jasperreports.engine.design.JRAbstractCompiler.loadEvaluator(JRAbstractCompiler.java:327)
    at net.sf.jasperreports.engine.design.JRDefaultCompiler.loadEvaluator(JRDefaultCompiler.java:217)

  23. Hi,

    I am a newbie to iReport. I am using iReport2.0.5 and jasper reports 3.0. I was able to establish the JDBC connection successfully. I am using a simple query to fetch the records from a table using the Report Wizard. It is throwing an error “SQL Problem: invalid object name table_name” . Can anyone suggest what could be the issue…

    Thanks alot

  24. Hi Rajib,

    Where do I get some information on using variables and Java formulas. Example;

    new Double($F{order_items_price_each}.doubleValue() * $F{material_items_weight_lbs}.doubleValue() / 100)

    gives compiler error —> / 100

    Thanking you in advance.

  25. Hi Rajib & friends,

    I have some questions regarding iReport and Java through eclipse.

    I want to create like web project and generate the reports(by using Java + iReport + MySql database) based on the user selection.

    How to do that,can u tell me please…..
    It’s very urgent for me.

    Thanks & Regards,
    Srinivas.

  26. Hi I am new to JasperReports and I tried to make the upper example, but the problem is that in the exported file, I can see only static text from the “jasper” file, not the data from the database. Please would you can tell me why?
    Thanks in advance!

  27. hi,
    i have some problm in ireport when i am using sql.

    WHERE
    bill.BillId = ( SELECT MAX(bill.BillId) FROM bill )
    and payment.PayNo = ( SELECT MAX(payment.PayNo) FROM payment )
    this is my where condition in my sqlquery.when i am executing this,it should display max billId and Max PayNo and in the next record will come to next page.
    if it is taking 3rd record,in that next page will come next record.

    How to do that….Help me

  28. Hi .. i am new to Jasper Reports. i am using Ireports and eclipse. can you please help me, by directing me to a good tutorial on how to create a report.

    The Ireport version i am using is 3.6.0 ..

    and i couldn’t find these in this version of Ireport ..

    Next-> select all columns. Next -> (do not select any group 1 value, if you want to show the report by any group you can select any column.) Next -> Tabular Layout (ClassicT) Finish.

  29. Hi Every one …

    I am using iReport, I have two groups which i use to suppress the data when they are repeting. When I use both together it is not suppressing both .. But when I use only one it suppress correctly .. Any input.

    Thanks a lot

  30. hi rajib,

    i m making report with struts 2. and i am getting this error
    ***********
    Servlet.service() for servlet default threw exception
    java.lang.NoClassDefFoundError: net/sf/jasperreports/engine/JRExporterParameter
    *****************

    although i havent tried your code yet.
    but, will it work for struts 2.0 framework ??
    please let me know ….

    thanks,
    chintan

  31. Hi Rajib,
    Can you please give me the code of the webproject to run the Jasper report + eclipse + struts.Means how to run step by step…..

    Thanks,
    Regards,
    Rajat

  32. Hello,
    i m using eclipse 3.4 ,JBoss and ireport 3.6.
    Database contains unicode data (Used font mangal.ttf)
    In Jasperviewer it is showing me the pdf file generated.But when call from the java file gives error

    net.sf.jasperreports.engine.JRRuntimeException: Could not load the following font :
    pdfFontName : mangal.TTF
    pdfEncoding : Identity-H
    isPdfEmbedded : true

    These setting i have done while creating report in ireport
    Please help me

  33. i am new to jasperreport,using ireport3.6, when i try to preview the file. it ask me for the device_instance_id ,where can i get that

  34. Hi,
    I need to list out the total days of the month with an input parameter of a particular date/month.
    Example

    input parameter – 14/10/2009 or 10/2009
    display in ireport

    1/10/2009
    2/10/2009
    3/10/2009


    31/10/2009

    Please advise

  35. Hi Jajib,

    Your Pie Chart example of iReport in mindblowing.It would be great if you provide an example of Stacked Bar and Line Chart as well.Looking forware to Hear you.

    Thank you
    -Aziz

  36. Hi,

    I am using jasper reports to get pdf reports.I have
    some fields in report which I want editable, so user
    can enter data in those.

    How can I do this(filling fields in pdf).I will really
    appricaite your help.

    Thanks,

  37. how to do or operator in IReport ?

    example :(($V{monthsname}.equals(“Mar 2010”)?$V{april}:$V{NANULL}) ||
    ($V{monthsname}.equals(“Mar 2010”)?$V{april}:$V{NANULL}) )

    iam getting error the operator || is undefined for the arguement type(s) java.lang.String

    plz solve this.

  38. how to pass dynamic where clause in jrxml.so that i can generate reports without changing query for each and every report.
    Thanks

  39. Hi Rajib,
    Thank You very much for your article.Really its a very nice article.I have gone through your sample and i just run the application it is working fine..But i want the sample spring project for jasper reports.Means i generated the jrxml files using ireport and i stucked at how to integrate these jrxml files with the spring application.I think you got my problem.I am new to spring,jasper reports i am using spring 3.0.5.I have gone through the internet for the samples but i didnt find the clear samples.I felt that your sample is very clear thats why i am asking the same for spring application.Its very urgent….Can you please help me..

  40. hi

    I am using iReport from outside and implemneting it in eclipse.
    Hi i have creates an report folder in webcontent.and in that all the reports are their.
    I want to open a report viewer when i run my project in tomcat.
    But when i do so.
    it giving file not found exception.
    Please let me know how could i fix it.

    Thanks in advance.

Leave a Reply

Your email address will not be published. Required fields are marked *