如何避免获取java.lang.NoClassDefFoundError:org / apache / commons / logging / LogFactory

时间:2013-06-14 17:35:41

标签: java jasper-reports

我有一个 NetBeans 项目,我想在其中设计jasper报告,当我通过 JasperFillManager.fillReport 填写报告时,我发现了一条错误消息。

我该如何解决这个问题?

我使用了 iReport 5.0.0 JasperReports库5.1.0

我的代码如下所述

try {
    String reportName = "./rptJobList.jasper";
    InputStream is = new FileInputStream(reportName);
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/js001?user=root&password=admin");
    JasperPrint jasperPrint = JasperFillManager.fillReport(is, null, con);
    JasperViewer jv = new JasperViewer(jasperPrint);
    jv.setVisible(true);
} catch (Exception ex) {
}

例外是:

  

线程“AWT-EventQueue-0”中的异常java.lang.NoClassDefFoundError:   org / apache / commons / logging / LogFactory at   net.sf.jasperreports.engine.util.JRLoader。(JRLoader.java:68)     在   net.sf.jasperreports.engine.JRPropertiesUtil.loadProperties(JRPropertiesUtil.java:99)     在   net.sf.jasperreports.engine.DefaultJasperReportsContext.initProperties(DefaultJasperReportsContext.java:94)     在   net.sf.jasperreports.engine.DefaultJasperReportsContext。(DefaultJasperReportsContext.java:71)     在   net.sf.jasperreports.engine.DefaultJasperReportsContext。(DefaultJasperReportsContext.java:59)     在   net.sf.jasperreports.engine.JasperFillManager.getDefaultInstance(JasperFillManager.java:85)     在   net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:824)     at job.NewClass.jButton2ActionPerformed(NewClass.java:64)at   job.NewClass.access $ 000(NewClass.java:21)at   job.NewClass $ 1.actionPerformed(NewClass.java:42)at   javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)     在   javax.swing.AbstractButton中的$ Handler.actionPerformed(AbstractButton.java:2318)     在   javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)     在   javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)     在   javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)     在java.awt.Component.processMouseEvent(Component.java:6134)at   javax.swing.JComponent.processMouseEvent(JComponent.java:3265)at   java.awt.Component.processEvent(Component.java:5899)at   java.awt.Container.processEvent(Container.java:2023)at   java.awt.Component.dispatchEventImpl(Component.java:4501)at   java.awt.Container.dispatchEventImpl(Container.java:2081)at   java.awt.Component.dispatchEvent(Component.java:4331)at   java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4301)     在   java.awt.LightweightDispatcher.processMouseEvent(Container.java:3965)     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)     在java.awt.Container.dispatchEventImpl(Container.java:2067)at   java.awt.Window.dispatchEventImpl(Window.java:2458)at   java.awt.Component.dispatchEvent(Component.java:4331)at   java.awt.EventQueue.dispatchEvent(EventQueue.java:599)at   java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)     在   java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)     在   java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)     在   java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)     在   java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)     在java.awt.EventDispatchThread.run(EventDispatchThread.java:122)   引起:java.lang.ClassNotFoundException:   org.apache.commons.logging.LogFactory at   java.net.URLClassLoader $ 1.run(URLClassLoader.java:200)at   java.security.AccessController.doPrivileged(Native Method)at   java.net.URLClassLoader.findClass(URLClassLoader.java:188)at   java.lang.ClassLoader.loadClass(ClassLoader.java:307)at   sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:301)at at   java.lang.ClassLoader.loadClass(ClassLoader.java:252)at   java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)... 35   更

1 个答案:

答案 0 :(得分:0)

需要将

Commons Logging添加到类路径中。如果您正在使用常规Netbeans项目(而不是Netbeans中的Maven项目),请查看解释如何将库添加到项目的this SO questionthis article

另一方面,如果您正在使用Netbeans处理Maven项目,则必须将Commons日志记录添加为依赖项。如何找到它here - 基本上它右键单击项目树中的库节点,添加依赖关系,输入人工制品细节或使用内置搜索助手找到正确的Maven存储库中的人工制品。