无法使jasper报告工作-获取java.lang.NoClassDefFoundError:net / sf / jasperreports / engine / JasperCompileManager

时间:2019-04-23 10:36:29

标签: java maven jasper-reports

我已经在这里停留了一段时间。我正在尝试使jasper报告工作,但出现以下错误:

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

我不明白原因。这是我的项目层次结构的样子:

enter image description here

CommandLineRunner是调用JasperCSVDataSource的主要类。

这是我的CommandLineRunner类的样子:

package com.zetcode.main;

public class CommandLineRunner {

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

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

导致错误的类JasperCSVDataSource是:

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);
        }
    }
}

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>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>

要运行代码,请执行以下操作:

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

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我们也曾在此上花费了数小时。我们花了很多时间试图找到类路径问题,而所有的调整都无济于事。对于我们来说,此修复程序正在添加:

-Djava.awt.headless=true

到启动JVM的命令行。 This SO answer为我们指明了正确的方向。

在我们的案例中,我们没有在日志中看到任何与sun/awt相关的异常来提供线索,但是无论如何,此修复程序仍然有效。