Saturday, October 23, 2010

Jasper Report and Netbeans Integration

 In this solution I am referring  

Installation of the programs listed above is a prerequisite .

At first run NetBeans IDE and go to tools >> Libraries then click on New Library.. . enter a name you prefer and set the Library Type as Class Library. After creating this , the name  is enlisted in the Libraries list . Select the name from that list . You will find Classpath tab for the library to be empty. Click on Add JAR/Folder....

then go to the iReport installation directory
then find

.......\ireport\modules\ext  directory

here you will find the following JAR files

  • commons-beanutils-1.8.2.jar
  • commons-collections-3.2.1.jar
  • commons-digester-1.7.jar
  • commons-javaflow-20060411.jar
  • commons-logging-1.1.jar
  • groovy-all-1.5.5.jar
  • iText-2.1.7.jar
  • jasperreports-3.7.5.jar


Add the files to the classpath tab you opened. After that create a new Project and add the library you created. Now you are ready to fly with reporting.
( these are the basic needs for netbeans project to be compatible with reporting ).


Use iReport designer to create the .jrxml file and place it in a directory say C:\Reports\ .  For example let the filename is sample.jrxml and an output file be C:\Reports\output\result.pdf .

Then place the following code inside main() of the project you added the library. Then simply run and see what happens.

try
{
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hello","hello","hello");
   
    JasperReport jasperReport = JasperManager.compileReport("C:/Reports/sample.jrxml");
   
    JasperPrint print = JasperManager.fillReport(jasperReport, new HashMap(), conn );    

    JasperExportManager.exportReportToPdfFile(print,"C:/Reports/output/result.pdf");
}

catch(JRException ex)
{
    System.out.println(ex.getMessage());
}

catch(SQLException ex )
{
    System.out.println(ex.getMessage());
}

catch(ClassNotFoundException ex )
{
    System.out.println(ex.getMessage());
}

File directories used in  the code ar specified as I assumed before the code segment.
Also change the connection string and DBMS according to yours.

11 comments:

  1. Hi..
    Would you mention how a jrxml file can be put in netbeans project itself and how to call that jrxml file?(Do database connections needed,just a simple jrxml file showing code is enough)
    Thanks

    ReplyDelete
  2. Hi..
    Would you mention how to put a jrxml file into the netbeans project and how to call that jrxml file(the path etc..)(Simple jrxml file and no database connection is needed.A working sample code is enough)

    ReplyDelete
  3. SEVERE: Parse Error at line 2 column 389: Document root element "jasperReport", must match DOCTYPE root "null".
    org.xml.sax.SAXParseException: Document root element "jasperReport", must match DOCTYPE root "null".
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.rootElementSpecified(XMLDTDValidator.java:1621)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(XMLDTDValidator.java:1900)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(XMLDTDValidator.java:764)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1357)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$ContentDriver.scanRootElementHook(XMLDocumentScannerImpl.java:1289)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3084)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:912)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:645)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:508)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
    at org.apache.commons.digester.Digester.parse(Digester.java:1647)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML(JRXmlLoader.java:239)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML(JRXmlLoader.java:226)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:214)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:168)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:152)
    at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:151)
    at Mass.main(Mass.java:82)
    org.xml.sax.SAXParseException: Document root element "jasperReport", must match DOCTYPE root "null".BUILD SUCCESSFUL (total time: 3 seconds)


    plz help me sir...hanging at end of project

    ReplyDelete
    Replies
    1. Hi I personally didn't face this type of situation. It might be caused due to formatting of the .jrxml file. Check if the tags in the .jrxml file are well formatted.You may take help from the following link
      http://blog.bangbits.com/2008/11/jasper-reports-and-legacy-formats.html

      Delete
  4. No such .jar file found in the module folder.

    ReplyDelete
  5. I could not even import net.sf.engine.print, what could I do to configure netbeans to work with jasperreport

    ReplyDelete
    Replies
    1. Hello,
      I haven't faced such problems. See if including jasperreports-applet.jar file to the class path can help you.

      Delete
    2. See if the package name is correct. I think it is net.sf.jasperreports.engine.print

      Delete
  6. @Sayeed Salam,
    Thanks for reply, you right about net.sf.jasperreports.engine.print, I missed when put comment here, my netbeans is 7.3.1 and it just said error net.sf.jasperreports.engine.print is not exist. if I typed net.sf. it will refer to net.sourceforge.
    sorry for my english.

    ReplyDelete
  7. Ok, my problems has solved already. thanks

    ReplyDelete