Java 使用库 JasperReport 版本 6.3

Using library JasperReport Version 6.3 for Java

我需要使用 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/ but now the function loadObject is deprecated. Searching a posible solution a found in http://amanuva.blogspot.cl/2016/10/060-conexion-con-informes-desde-java.html 更新到 6.3.0

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

欢迎提出任何建议

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