Error on Running java code to generate Blank jasper report automatically

I tried to run a blank Jasperreport (Blank A4.Jrxml) using Jaspersoft studio 6.4.0 using Eclipse Neon JAVA EE IDE. Every time I run this code I was asked to add a jasper jar dependency file. So I downloaded Jasper library and Jasper jar files into my Java program (JasperClass.java) and saved in the lib folder. But still I a getting errors to add jar files. I looked into stack flow previous solutions but still, I could not resolve it. Please help me on this.

JAR library in Lib folder: https://imgur.com/Nj1FOGk

Error:

Oct 11, 2017 1:01:28 PM net.sf.jasperreports.extensions.DefaultExtensionsRegistry loadRegistries
WARNING: Extension resource jar:file:/C:/repo/JasperTest/lib/jasperreports-javaflow-6.4.0.jar!/jasperreports_extension.properties was found to be a duplicate of jar:file:/C:/repo/JasperTest/lib/jasperreports-6.4.0.jar!/jasperreports_extension.properties in classloader sun.misc.Launcher$AppClassLoader@73d16e93
Exception in thread "main" java.lang.NoClassDefFoundError: com/lowagie/text/DocumentException
    at net.sf.jasperreports.engine.JasperExportManager.exportToPdfFile(JasperExportManager.java:150)
    at net.sf.jasperreports.engine.JasperExportManager.exportReportToPdfFile(JasperExportManager.java:503)
    at JasperPackage.JasperClass.main(JasperClass.java:45)
Caused by: java.lang.ClassNotFoundException: com.lowagie.text.DocumentException
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 3 more

Java Class.Java

import java.io.File;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;

    import net.sf.jasperreports.engine.JRDataSource;
    import net.sf.jasperreports.engine.JREmptyDataSource;
    import net.sf.jasperreports.engine.JRException;
    import net.sf.jasperreports.engine.JasperCompileManager;
    import net.sf.jasperreports.engine.JasperExportManager;
    import net.sf.jasperreports.engine.JasperFillManager;
    import net.sf.jasperreports.engine.JasperPrint;
    import net.sf.jasperreports.engine.JasperReport;
    public class JasperClass {

       public static void main(String[] args) throws JRException, IOException {

            // Compile jrxml file.
           JasperReport jasperReport = JasperCompileManager
                   .compileReport("C:/repo/JasperTest/Blank_A4.jrxml");

           // Parameters for report
           Map<String, Object> parameters = new HashMap<String, Object>();

           // DataSource
           // This is simple example, no database.
           // then using empty datasource.
           JRDataSource dataSource = new JREmptyDataSource();

           JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
                   parameters, dataSource);


           // Make sure the output directory exists.
           File outDir = new File("C:/repo/JasperTest");
           outDir.mkdirs();

           // Export to PDF.
           JasperExportManager.exportReportToPdfFile(jasperPrint,
                   "C:/repo/JasperTest/Blank.pdf");

           System.out.println("Done!");
       }
    }

Blank A4.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.4.0.final using JasperReports Library version 6.4.1  -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Blank_A4" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="1b2e21df-4dba-46a0-8e2e-1bed8b81853e">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="125" splitType="Stretch"/>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

2 answers

  • answered 2017-10-11 10:15 Space Cadet

    Based on the exception in your stack trace you appear to have a duplicate JAR file in your repository lib directory

    jasperreports-javaflow-6.4.0.jar jasperreports-6.4.0.jar

    Are both of those JARs necessary?

  • answered 2017-10-11 10:15 saleem

    Thank you for your help. My problem resolved.

    1. I removed Javaflow6.4.0 jar instead of Jasperreport 6.4.0 jar from lib folder. For the exception

      Oct 11, 2017 1:01:28 PM net.sf.jasperreports.extensions.DefaultExtensionsRegistry loadRegistries WARNING: Extension resource jar:file:/C:/repo/JasperTest/lib/jasperreports-javaflow-6.4.0.jar!/jasperreports_extension.properties was found to be a duplicate of jar:file:/C:/repo/JasperTest/lib/jasperreports-6.4.0.jar!/jasperreports_extension.properties in classloader sun.misc.Launcher$AppClassLoader@73d16e93 Exception in thread "main" java.lang.NoClassDefFoundError: com/lowagie/text/DocumentExceptio

    2. Later I got an Exception as below. Then I added Itext 2.1.7 jar along with itext pdf 5.5.0 jar into the lib file. And this worked. Exception in thread "main" java.lang.NoClassDefFoundError: com/lowagie/text/DocumentException at net.sf.jasperreports.engine.JasperExportManager.exportToPdfF‌​ile(JasperExportMana‌​ger.java:150)

    Thank you again.