0

嗨,我对碧玉报告很陌生。只是有一个要求,我需要从 .jasper 文件中检索信息。我正在使用下面的代码。

但问题是 compileReport 不占用我在本地机器上的文件,我收到类似的错误

线程“main”中的异常 java.lang.NoClassDefFoundError: org/apache/commons/digester/Digester at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:150) at jasper.test(jasper.java:28 ) 在 jasper.main(jasper.java:40)

我不确定出了什么问题。它无法获取我的 .jasper 文件吗?哪一门课不见了?我完全一无所知。

我的代码是:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import dori.jasper.engine.JRException;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.view.JasperViewer;


public class jasper  {

    public static void test() throws FileNotFoundException, net.sf.jasperreports.engine.JRException{

    JasperReport report = JasperCompileManager.compileReport("C:\\Users\\Mandakini\\Downloads\\testReport.jasper");

    JasperPrint print = JasperFillManager.fillReport(report, new HashMap<String, String>());
    // export it!
    //File pdf = File.createTempFile("output.", ".pdf");

    OutputStream output= new FileOutputStream(new File("F:/catalog.pdf"));
    JasperExportManager.exportReportToPdfStream(print,output);

    }

    public static void main(String args[]) throws FileNotFoundException, net.sf.jasperreports.engine.JRException{
        test();
    }
}

谢谢

4

1 回答 1

0

A java.lang.NoClassDefFoundErrororClassNotFoundException应该几乎总是告诉您它引用的类在您的类路径中丢失。在这种情况下,它是org.apache.commons.digester.Digester. 您可以在此处下载包含此类的 jar 。

要生成报告,请执行以下操作:

JasperReport jasperReport = (JasperReport) JRLoader.loadObjectFromFile(yourJasperFilePath);

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

Class.forName("com.mysql.jdbc.Driver");
Connection con = ...; // possibly get a connection

byte[] report = JasperRunManager.runReportToPdf(jasperReport, parameters, con); // pass the report, the report parameters, and a connection 
// this will fill the byte[] with the produced report

FileOutputStream out = new FileOutputStream(somePath);
out.write(report);
out.close();

至于它生成什么样的报告,我不确定你是如何设置的。该信息可能在.jasperor.jrxml文件中。

于 2013-05-10T15:44:05.583 回答