Boa noite!
Segui um tutorial do GUJ sobre iReports, e minha classe não apresenta erros. Na hora de gerar o aplicativo aparece o seguinte erro:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at net.sf.jasperreports.engine.util.JRLoader.<clinit>(JRLoader.java:60)
at net.sf.jasperreports.engine.JasperFillManager.fillReportToFile(JasperFillManager.java:458)
at RelatorioBibliotecaPessoal.geraRelatorio(RelatorioBibliotecaPessoal.java:38)
at RelatorioBibliotecaPessoal.main(RelatorioBibliotecaPessoal.java:49)
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 4 more
Java Result: 1
A classe com o método que gera o relatório:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.view.JasperViewer;
public class RelatorioBibliotecaPessoal {
public static Connection getConnection() throws SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Conectando ao banco");
return DriverManager.getConnection("jdbc:mysql://localhost/teste", "root", "key");
} catch (ClassNotFoundException e) {
throw new SQLException(e.getMessage());
}
}
/* Gera Relatorio e visualiza-o */
public void geraRelatorio() throws JRException, Exception {
Connection con = getConnection();
Statement stm = con.createStatement();
String query = "select * from livro";
ResultSet rs = stm.executeQuery(query);
/* implementação da interface JRDataSource para DataSource ResultSet */
JRResultSetDataSource jrRS = new JRResultSetDataSource(rs);
/* HashMap de parametros utilizados no relatório. Sempre instanciados */
Map parameters = new HashMap();
// parameters.put("COLUNA", valor);
/* Preenche o relatório com os dados. Gera o arquivo BibliotecaPessoal.jrprint */
JasperFillManager.fillReportToFile("livro.jasper", parameters, jrRS);
/* Exporta para o formato PDF */
JasperExportManager.exportReportToPdfFile("BibliotecaPessoal.jrprint");
/* Preenche o relatorio e o salva diretamente em arquivo PDF. Sem
a necessidade do .jrprint */
// JasperRunManager.runReportToPdfFile("BibliotecaPessoal.jasper", parameters, jrRS);
/* Visualiza o relatório em formato PDF */
JasperViewer.viewReport("BibliotecaPessoal.pdf", false);
}
public static void main(String[] args) throws JRException, Exception {
new RelatorioBibliotecaPessoal().geraRelatorio();
}
}
Alguma sugestão do que está causando este erro, ou alguma outra maneira de “chamar” este relatório?
Obrigado!