1

I am trying to get Jasper Reports to work, but I have been getting the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: net/sf/jasperreports/engine/JasperCompileManager
        at com.suhail.main.JasperCSVDataSource.start(JasperCSVDataSource.java:21)
        at com.suhail.main.CommandLineRunner.main(CommandLineRunner.java:8)
Caused by: java.lang.ClassNotFoundException: net.sf.jasperreports.engine.JasperCompileManager
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        ... 2 more

I cannot understand the reason for this. Here is how my project hierarchy looks like:

enter image description here

The class CommandLineRunner is the main class which invokes JasperCSVDataSource.

Here is my how CommandLineRunner class looks like:

package com.zetcode.main;

public class CommandLineRunner {

    public static void main(String[] args) throws Exception {

        JasperCSVDataSource app = new JasperCSVDataSource();
        app.start();
    }
}

and the class JasperCSVDataSource that results in an error is:

package com.zetcode.main;

import java.util.HashMap;
import java.util.Map;

import net.sf.jasperreports.engine.*;

public class JasperCSVDataSource {

    public void start() throws JRException {
        try {
            System.out.println("In here!");
            String xmlFile = "src/main/resources/report2.xml";
            JasperReport report = JasperCompileManager.compileReport(xmlFile);

            String[] columnNames = new String[] {"Name", "Quantity"};

             String fileName = "src/main/resources/items.csv";
             JRCsvDataSource ds = new JRCsvDataSource(fileName);
             ds.setColumnNames(columnNames);

             Map parameters = new HashMap();

             JasperPrint jprint = JasperFillManager.fillReport(report, parameters, ds);

             JasperExportManager.exportReportToPdfFile(jprint,
                     "src/main/resources/report2.pdf");
        }catch(Exception e) {
            System.out.println("Error!!");
            System.err.println(e);
        }
    }
}

The pom.xml looks as follows:

    <?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>com.zetcode</groupId>
    <artifactId>JasperCSVDataSource</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>

        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.8.0</version>
        </dependency>

    </dependencies>    

</project>

To run the code, I do the following:

mvn dependency:build-classpath
cd target/classes
java com/zetcode/main/CommandLineRunner

What is it that I am doing incorrect?

halfer
  • 19,824
  • 17
  • 99
  • 186
Amanda
  • 2,013
  • 3
  • 24
  • 57

1 Answers1

0

We spent hours on this once upon a time too. We spent a lot of time trying to find classpath issues and all of our adjustments didn't help. For us, the fix was adding:

-Djava.awt.headless=true

to the command line that starts the JVM. This SO answer pointed us in the correct direction.

In our case, we did not see any sun/awt related exceptions in our logs to give us a clue, but this fix worked anyway.

user944849
  • 14,524
  • 2
  • 61
  • 83
  • So I tried running `java -Djava.awt.headless=true com/suhail/main/CommandLineRunner` but it gives the same error. – Amanda Apr 23 '19 at 13:54
  • Just looked closer at how you built the app. Try running `mvn package` and then execute the jar. You're attempting to use a single class without providing the classpath. – user944849 Apr 23 '19 at 14:47
  • Still getting the same error – Amanda Apr 24 '19 at 07:00
  • I did `mvn package` and then tried to run the jar as `java -jar jarfile.jar` Is there anything else that I need to do. – Amanda Apr 24 '19 at 07:00