使用库JasperReport版本6.3 for Java

时间:2017-03-29 23:09:19

标签: java jasper-reports noclassdeffounderror

我需要使用JasperReport。我前段时间使用过IReport5.5,但现在使用Java 8 iReport并没有运行。相反,我安装了JasperSoftStudio 6.3.1来设计我的jrxml文件。

问题是在我使用jasperreport 5.2之前,它非常适合java代码:

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;
import net.sf.jasperreports.engine.JasperCompileManager;

public class Main {

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

        Class.forName("org.postgresql.Driver");
        Connection conexion = DriverManager.getConnection(
                "jdbc:postgresql://127.0.0.1:5432/database", "postgres",
                "password");


        JasperCompileManager.compileReportToFile(
                "input.jrxml",
                "input.jasper");

        JasperReport reporte = (JasperReport) JRLoader.loadObject("input.jasper");
        JasperPrint jasperPrint = JasperFillManager.fillReport(reporte, null, conexion);

        JRExporter exporter = new JRPdfExporter();

        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_FILE, new java.io.File("output.pdf"));
        exporter.exportReport();


    }
}

但是,使用JasperSoftStudio设计我的报告时,旧版本的jasperreport 5.2不接受它。我将jasperversion从https://sourceforge.net/projects/jasperreports/files/jasperreports/更新为6.3.0,但现在不推荐使用函数 loadObject 。按以下方式搜索http://amanuva.blogspot.cl/2016/10/060-conexion-con-informes-desde-java.html中找到的可行解决方案

import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;


public class JavaMainClass {
    public static void main(String[] argv) throws Exception {
        Class.forName("org.postgresql.Driver");
        Connection conexion = DriverManager.getConnection(
                "jdbc:postgresql://127.0.0.1:5432/database", "postgres",
                "password");

        JasperReport jasperReport = JasperCompileManager.compileReport("input.jrxml");
        JasperPrint jprint = (JasperPrint) JasperFillManager.fillReport("input.jasper", null, conexion);
        JasperExportManager.exportReportToPdfFile(jprint, "output.pdf");

    }
}

但是,我收到此错误

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at net.sf.jasperreports.engine.util.JRLoader.<clinit>(JRLoader.java:81)
    at net.sf.jasperreports.engine.JRPropertiesUtil.loadProperties(JRPropertiesUtil.java:102)
    at net.sf.jasperreports.engine.DefaultJasperReportsContext.initProperties(DefaultJasperReportsContext.java:99)
    at net.sf.jasperreports.engine.DefaultJasperReportsContext.<init>(DefaultJasperReportsContext.java:76)
    at net.sf.jasperreports.engine.DefaultJasperReportsContext.<clinit>(DefaultJasperReportsContext.java:59)
    at net.sf.jasperreports.engine.JasperCompileManager.getDefaultInstance(JasperCompileManager.java:200)
    at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:575)
    at JavaMainClass.main(JavaMainClass.java:57)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 13 more

欢迎任何建议

1 个答案:

答案 0 :(得分:0)

您似乎错过了类路径中的Apache commons-logging库。你使用像Maven这样的依赖管理工具吗?如果没有,那可能是开始的好时机...... :)