1
votes

I'm trying to get the data that I have in a JTable and print it like a report using Jasper Reports. I've found Usando o JRBeanCollectionDataSource and Java beans as Jasper Reports data source using JRBeanCollectionDataSource as examples to create my own code:

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

    }
}

This class is called when the ActionEvent occurs:

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

This is my trace:

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

I thought I had all the necessary libraries for Jasper Reports, which one am I missing?

1

1 Answers

1
votes

My guess is you are missing the Jakarta Commons Logging Component (version 1.0.4 or later) library on your classpath. You should probably take a look at the JasperReports Requirements to make sure you have all the other required libraries you need and the appropriate versions.

As an aside, you mentioned you data is in a JTable, you may want to take a look at JRTableModelDataSource which will let you us the Jtable's TableModel to create a datasource.