Sunday, May 17, 2020

Dev Tips#37 Create your first Jasper report in Java and VS code maven


In this tutorial, I show you how to create a simple jasper report with a static text in TIBCO Jasper Report Studio. Then run the jrmxl file in a Maven Java Project in visual studio code to get a pdf.

Main Class


   

package com.abc;
import java.util.HashMap;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.JREmptyDataSource;
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.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader; 
/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
      

        String fileNameJrxml = "E:/Jasper/HelloPDF.jrxml";
        String fileNamePdf = "E:/Jasper/HelloPDF.pdf";
 
        try {
            System.out.println("Loading the .JRMXML file ....");
            JasperDesign jasperDesign = JRXmlLoader.load(fileNameJrxml);
            System.out.println("Compiling the .JRMXML file to .JASPER file....");
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
            HashMap hm = new HashMap();
            System.out.println("filling parameters to .JASPER file....");
            JasperPrint jprint = (JasperPrint) JasperFillManager.fillReport(jasperReport, hm, new JREmptyDataSource());
            System.out.println("exporting the JASPER file to PDF file....");
            JasperExportManager.exportReportToPdfFile(jprint, fileNamePdf);
            System.out.println("Successfully completed the export");

        } catch (Exception e) {
            System.out.print("Exception:" + e);
        }

    }
}


  
  


List of dependencies for the pom.xml
    

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
<dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports</artifactId>
    <version>6.8.0</version>
</dependency>
  <dependency>
   <groupId>commons-beanutils</groupId>
   <artifactId>commons-beanutils</artifactId>
   <version>1.9.3</version>
   <scope>compile</scope>
   <optional>false</optional>
  </dependency>
  <dependency>
   <groupId>commons-digester</groupId>
   <artifactId>commons-digester</artifactId>
   <version>2.1</version>
   <scope>compile</scope>
   <optional>false</optional>
  </dependency>
  <dependency>
   <groupId>commons-logging</groupId>
   <artifactId>commons-logging</artifactId>
   <version>1.1.1</version>
   <scope>compile</scope>
   <optional>false</optional>
  </dependency>
  <dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-collections4</artifactId>
   <version>4.2</version>
   <scope>compile</scope>
   <optional>false</optional>
  </dependency>
    <dependency>
   <groupId>com.lowagie</groupId>
   <artifactId>itext</artifactId>
   <version>2.1.7.js6</version>
    <scope>compile</scope>
   <optional>false</optional>
   <exclusions>
    <exclusion>
     <groupId>org.bouncycastle</groupId>
     <artifactId>bcmail-jdk15on</artifactId>
    </exclusion>
    <exclusion>
     <groupId>org.bouncycastle</groupId>
     <artifactId>bcpkix-jdk15on</artifactId>
    </exclusion>
   </exclusions>
  </dependency>
 <dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi</artifactId>
   <version>4.0.1</version>
   <scope>compile</scope>
   <optional>true</optional>
  </dependency>
  <dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml</artifactId>
   <version>4.0.1</version>
   <scope>compile</scope>
   <optional>true</optional>
  </dependency>
<dependency>
   <groupId>org.eclipse.jdt.core.compiler</groupId>
   <artifactId>ecj</artifactId>
   <version>4.4.2</version>
   <scope>compile</scope>
   <optional>false</optional>
  </dependency>
  <dependency>
   <groupId>org.codehaus.groovy</groupId>
   <artifactId>groovy-all</artifactId>
   <version>2.4.5</version>
   <scope>compile</scope>
   <optional>true</optional>
  </dependency>
    <dependency>
   <groupId>org.apache.logging.log4j</groupId>
   <artifactId>log4j-core</artifactId>
   <version>2.8.2</version>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>org.apache.logging.log4j</groupId>
   <artifactId>log4j-jcl</artifactId>
   <version>2.8.2</version>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>net.sf.jasperreports</groupId>
   <artifactId>jasperreports-fonts</artifactId>
   <version>6.8.0</version>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>org.apache.lucene</groupId>
   <artifactId>lucene-core</artifactId>
   <version>7.3.0</version>
   <scope>compile</scope>
   <optional>true</optional>
  </dependency>
  <dependency>
   <groupId>org.apache.lucene</groupId>
   <artifactId>lucene-analyzers-common</artifactId>
   <version>7.3.0</version>
   <scope>compile</scope>
   <optional>true</optional>
  </dependency>
  <dependency>
   <groupId>org.apache.lucene</groupId>
   <artifactId>lucene-queryparser</artifactId>
   <version>7.3.0</version>
   <scope>compile</scope>
   <optional>true</optional>
  </dependency>
  <dependency>
   <groupId>org.olap4j</groupId>
   <artifactId>olap4j</artifactId>
   <version>0.9.7.309-JS-3</version>
   <scope>compile</scope>
   <optional>true</optional>
  </dependency>
    <dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-pool2</artifactId>
   <version>2.4.2</version>
   <scope>compile</scope>
   <optional>true</optional>
  </dependency>
  <dependency>
   <groupId>commons-codec</groupId>
   <artifactId>commons-codec</artifactId>
   <version>1.5</version>
   <scope>compile</scope>
   <optional>true</optional>
  </dependency>
  <dependency>
   <groupId>net.sf.jasperreports</groupId>
   <artifactId>jasperreports-metadata</artifactId>
   <version>6.8.0</version>
   <scope>compile</scope>
   <optional>true</optional>
  </dependency>

  </dependencies> 

  
  


No comments:

Post a Comment