Search code examples
javajasper-reportsnoclassdeffounderror

Got 'java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory` in code using JasperReports API


I am working on a simple standalone desktop application which would generate report based on the values passed to the program. There is no database usage. I have designed my JasperReports report with the iReport designer and added a parameter ID_NO and a text field with expression $P{ID_NO}

I can preview the report successfully and see what I expected. But I cant generate the report from Java application and pass the parameter ID_NOfrom there. Here is my code:

public class MyReportViewer extends JFrame {

    public MyReportViewer(String fileName) {
        this(fileName,null);
    }

    public MyReportViewer(String fileName,HashMap<String, Object> parameter) {
        super("View Report");
        try {
            JasperPrint print = JasperFillManager.fillReport(fileName, parameter);
            JRViewer viewer = new JRViewer(print);

            Container c = getContentPane();
            c.add(viewer);
        } catch(Exception e) {
            e.printStackTrace();
        }

        setBounds(10, 10, 600,500);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    public static void main(String args[]) {
        HashMap<String, Object> param = new HashMap<String, Object>();
        param.put("ID_NO", "101-15-980");
        MyReportViewer viewer = new MyReportViewer("test.jasper", param);
        viewer.setVisible(true);
    }
}

The code gives a exception

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at net.sf.jasperreports.engine.util.JRLoader.<clinit>(JRLoader.java:61)
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:267)
    at jspertest.MyReportViewer.<init>(MyReportViewer.java:37)
    at jspertest.MyReportViewer.main(MyReportViewer.java:60)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)

And does not run.

Please give me a clue. I have never used JasperReports or any kind of report generator before.

Edit:

Added all the library files from jasperreports4.x.x/lib/.. to the project. Now the exception is gone. Now the console says-

log4j:WARN No appenders could be found for logger (net.sf.jasperreports.extensions.ExtensionsEnvironment).
log4j:WARN Please initialize the log4j system properly.

and a small popup says there are no pages in the document. A small windows appears with no report in it.


Solution

  • Your exception is not related with the Parameter issue, which apparently looks good.

    It looks like you are missing Apache Commons Logging in your classpath. It runs in preview mode because your IDE does have the correct classpath.