Search code examples
javajasper-reports

Jasper Reports throws Exception when running JAR but working PERFECTLY in IntelliJ IDE


The Jar file runs perfectly for other components in report like field/parameters/variables. But for 'Table' component, PDF is generated perfectly in IDE but Jar file throws Exception.

Main Problem: Including ' Table ' is causing Error. Works in IDE but NOT in Jar.

Java: 21.0.5 Jasper: 7.0.1

log:

PS C:\Users\hp\IdeaProjects\sampfxone> java -jar .\target\sampfxone-1.0-SNAPSHOT.jar
Exception in thread "main" 
Exception: net.sf.jasperreports.engine.JRRuntimeException thrown from the UncaughtExceptionHandler in thread "main"
PS C:\Users\hp\IdeaProjects\sampfxone> 

Main.Java

package org.example;

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.view.JasperViewer;
import java.awt.*;
import java.io.InputStream;
import java.util.*;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        
        getTempl();
    }

    public static void getTempl() {

        Map<String, Object> parameters = new HashMap<>();

        List<Invoice> singleInvoice = new ArrayList<>();
        singleInvoice.add(new Invoice("asad","psa","qas", "ssr", 0F));
        JRBeanCollectionDataSource invoicesDataSource = new JRBeanCollectionDataSource(singleInvoice, false);

        parameters.put("BillDatasetSource", invoicesDataSource);

        JasperPrint jasperPrint;

        InputStream resourceStream = Main.class.getResourceAsStream("/templates/v.jasper");

        try {
            jasperPrint = JasperFillManager.fillReport(resourceStream, parameters, new JREmptyDataSource());
            JasperViewer jv = new JasperViewer(jasperPrint, false);
            jv.setZoomRatio(Component.CENTER_ALIGNMENT);
            jv.setVisible(true);
            jv.setExtendedState(JasperViewer.MAXIMIZED_BOTH);

        } 
        catch (JRException e) {
            throw new RuntimeException(e);
        }
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>sampfxone</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports-pdf</artifactId>
            <version>7.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-csv</artifactId>
            <version>1.13.0</version>
        </dependency>

        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports-jdt</artifactId>
            <version>7.0.1</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.6.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>org.example.Main</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

v.jrxml

<!-- Created with Jaspersoft Studio version 7.0.1.final using JasperReports Library version 7.0.1-573496633c2b4074e32f433154b543003f7d2498  -->
<jasperReport name="v" language="java" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="b5b6a90f-ace6-4844-8aff-720b7cfd0bcb">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <dataset name="BillDataset" uuid="51fdc3db-53fa-437e-adf7-22c399fcb755">
        <query language="sql"><![CDATA[]]></query>
    </dataset>
    <parameter name="BillDatasetSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <query language="sql"><![CDATA[]]></query>
    <background splitType="Stretch"/>
    <title height="79" splitType="Stretch"/>
    <pageHeader height="35" splitType="Stretch"/>
    <columnHeader height="253" splitType="Stretch">
        <element kind="component" uuid="cd328d63-469c-4144-8246-723bcc673693" x="180" y="70" width="200" height="90">
            <component kind="table">
                <datasetRun uuid="ee18fb01-32a9-4845-b5e7-e2a3bff73c89" subDataset="BillDataset">
                    <dataSourceExpression><![CDATA[$P{BillDatasetSource}]]></dataSourceExpression>
                </datasetRun>
                <column kind="single" uuid="8b880b93-35b5-40c9-af3e-b4ccfc5e56fb" width="200">
                    <tableFooter height="30" style="Table_TH"/>
                    <columnHeader height="30" style="Table_CH"/>
                    <detailCell height="30" style="Table_TD"/>
                </column>
            </component>
            <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
            <property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/>
            <property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/>
            <property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/>
        </element>
    </columnHeader>
    <detail>
        <band height="125" splitType="Stretch"/>
    </detail>
    <columnFooter height="45" splitType="Stretch"/>
    <pageFooter height="54" splitType="Stretch"/>
    <summary height="42" splitType="Stretch"/>
</jasperReport>


Solution

  • pom.xml

    • remove plugin maven-shade-plugin
    • set finalName to app
    • add dependency: jasperreports
    • add dependency: log4j-api
    • add dependency: log4j-core
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>org.example</groupId>
        <artifactId>sampfxone</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <properties>
            <log4j2.version>2.23.1</log4j2.version>
            <maven.compiler.source>21</maven.compiler.source>
            <maven.compiler.target>21</maven.compiler.target>
            <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>net.sf.jasperreports</groupId>
                <artifactId>jasperreports-pdf</artifactId>
                <version>7.0.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-csv</artifactId>
                <version>1.13.0</version>
            </dependency>
    
            <dependency>
                <groupId>net.sf.jasperreports</groupId>
                <artifactId>jasperreports-jdt</artifactId>
                <version>7.0.1</version>
            </dependency>
    
            <dependency>
                <groupId>net.sf.jasperreports</groupId>
                <artifactId>jasperreports-excel-poi</artifactId>
                <version>7.0.1</version>
            </dependency>
    
            <dependency>
                <groupId>net.sf.jasperreports</groupId>
                <artifactId>jasperreports</artifactId>
                <version>7.0.1</version>
            </dependency>
    
            <!-- Log4j2 core -->
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>${log4j2.version}</version>
            </dependency>
    
            <!-- Log4j2 API -->
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
                <version>${log4j2.version}</version>
            </dependency>
    
        </dependencies>
    
        <build>
            <finalName>app</finalName>
        </build>
    
    </project>
    

    src\main\resources\log4j2.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="warn">
        <Appenders>
            <!-- Console Appender Configuration -->
            <Console name="Console" target="SYSTEM_OUT">
                <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
            </Console>
    
            <!-- File Appender Configuration -->
            <File name="File" fileName="app.log" append="true">
                <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
            </File>
        </Appenders>
    
        <Loggers>
            <!-- Root Logger Configuration -->
            <Root level="debug">
                <AppenderRef ref="Console"/>
                <AppenderRef ref="File"/>
            </Root>
        </Loggers>
    </Configuration>
    

    Build and Download dependencies

    mvn clean package
    
    mvn dependency:copy-dependencies -DoutputDirectory=target\libs
    

    Output Directory

    • your code: target\app.jar
    • dependencies: target\libs\xxx.jar
    target
    ├── app.jar
    ├── libs
    │   ├── antlr-2.7.7.jar
    │   ├── batik-anim-1.17.jar
    │   ├── batik-awt-util-1.17.jar
    ...
    ...
    │   ├── xmlbeans-5.0.3.jar
    │   ├── xmlgraphics-commons-2.9.jar
    │   └── xmpcore-6.1.11.jar
    

    Run

    if your Main.java have package name: org.example

    java -cp "target\libs\*;target\app.jar" org.example.Main
    

    If your Main.java does not have package.

    java -cp "target\libs\*;target\app.jar" Main
    

    target\app.jar

    Check your app.jar and there should be org\example\Main.class

    target\app.jar
    ├── log4j2.xml
    ├── META-INF
    │   ├── MANIFEST.MF
    ...
    ├── org
    │   └── example
    │       └── Main.class
    ...
    

    My Example Code

    Report File is from https://github.com/TIBCOSoftware/jasperreports/blob/master/demo/samples/table/reports/TableReport.jrxml

    My Example Code