1

我正在尝试获取 JTable 中的数据并像使用 Jasper Reports 的报告一样打印它。我发现Usando o JRBeanCollectionDataSourceJava bean 作为 Jasper Reports 数据源,使用 JRBeanCollectionDataSource作为示例来创建我自己的代码:

package Negocio.Relatorios;

import Negocio.Contato;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.view.JasperViewer;

public class DataFromRelatorios{

    public void exibirRelatorio(List<Contato> contatos){

        String caminhoJasper = "./etiquetas.jasper";
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(contatos);
        Map parameters = new HashMap();
        JasperPrint printer;
        InputStream relJas = getClass().getResourceAsStream(caminhoJasper);

        try {
            printer = JasperFillManager.fillReport(relJas, parameters, dataSource);
            JasperViewer viewer = new JasperViewer(printer, true);
            viewer.setVisible(true);
        }
        catch (JRException ex) {
            ex.printStackTrace();
        } 

    }
}

当 ActionEvent 发生时调用此类:

private void btnGeraEtiquetasActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        geradorRelatorios.exibirRelatorio(relatorioTableModel.getDados());
    }  

这是我的踪迹:

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at net.sf.jasperreports.engine.util.JRLoader.<clinit>(JRLoader.java:65)
    at net.sf.jasperreports.engine.JRPropertiesUtil.loadProperties(JRPropertiesUtil.java:99)
    at net.sf.jasperreports.engine.DefaultJasperReportsContext.initProperties(DefaultJasperReportsContext.java:95)
    at net.sf.jasperreports.engine.DefaultJasperReportsContext.<init>(DefaultJasperReportsContext.java:72)
    at net.sf.jasperreports.engine.DefaultJasperReportsContext.<clinit>(DefaultJasperReportsContext.java:60)
    at net.sf.jasperreports.engine.JasperFillManager.getDefaultInstance(JasperFillManager.java:85)
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:956)
    at Negocio.Relatorios.DataFromRelatorios.exibirRelatorio(DataFromRelatorios.java:25)
    at GUI.JanGerenciaContato.btnGeraEtiquetasActionPerformed(JanGerenciaContato.java:239)
    at GUI.JanGerenciaContato.access$300(JanGerenciaContato.java:21)
    at GUI.JanGerenciaContato$4.actionPerformed(JanGerenciaContato.java:104)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6288)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6053)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.awt.Component.dispatchEventImpl(Component.java:4651)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4481)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.java:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4481)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
    at java.awt.EventQueue.access$000(EventQueue.java:84)
    at java.awt.EventQueue$1.run(EventQueue.java:602)
    at java.awt.EventQueue$1.run(EventQueue.java:600)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:616)
    at java.awt.EventQueue$2.run(EventQueue.java:614)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 47 more

我以为我拥有 Jasper Reports 所需的所有库,但我缺少哪个库?

4

1 回答 1

1

我的猜测是您的类路径中缺少Jakarta Commons Logging Component(版本 1.0.4 或更高版本)库。您可能应该查看JasperReports 要求,以确保您拥有所需的所有其他必需库和适当的版本。

顺便说一句,您提到您的数据在 JTable 中,您可能想看看JRTableModelDataSource它将让您使用 Jtable 的 TableModel 创建数据源。

于 2012-07-19T21:12:32.887 回答