Search code examples
javajasper-reportsclasspathnoclassdeffounderror

Getting NoClassDefFoundError when running JasperReports based application from command line


For the whole day I have been trying to run my first report based on the JasperReports library, but I am yet to get my desired result.

I have a ReportTemplate.jrxml file containing:

<!DOCTYPE jasperReport
  PUBLIC "-//JasperReports//DTD Report Design//EN"
  "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport name="Simple_Report">
 <detail>
    <band height="20">
      <staticText>
        <reportElement x="180" y="0" width="200" height="20"/>
        <text><![CDATA[Hello World!]]></text>
      </staticText>
    </band>
  </detail>
</jasperReport>

and my .java file:

import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JRException;
import java.util.HashMap;

public class TestReport {
    public static void main(String[] args)  {
        HashMap<String, Object> hm = new HashMap<>();
        JREmptyDataSource ds = new JREmptyDataSource();
        JasperReport jasperReport;
        JasperPrint jasperPrint;
        try {
            jasperReport = JasperCompileManager.compileReport("ReportTemplate.jrxml");
            jasperPrint = JasperFillManager.fillReport(jasperReport, hm, ds);
            JasperExportManager.exportReportToPdfFile(
            jasperPrint, "reports/simple_report.pdf");
        } catch (JRException e) {
            e.printStackTrace();
        }
    }
}

Both locate in the same directory. So, let's go

D:\Java\Applecation\Report>javac -classpath D:\JasperReports\jasperreports-5.5.0\dist\jasperreports-5.5.0.jar TestReport.java
D:\Java\Applecation\Report>_

works!

D:\Java\Applecation\Report>java -classpath D:\JasperReports\jasperreports-5.5.0\dist\jasperreports-5.5.0.jar TestReport
Error: Could not find or load main class TestReport

try simply

D:\Java\Applications\ATConsulting\report>java TestReport
Exception in thread "main" java.lang.NoClassDefFoundError: net/sf/jasperreports/
engine/JRException
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2521)
        at java.lang.Class.getMethod0(Class.java:2764)
        at java.lang.Class.getMethod(Class.java:1653)
        at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)

Caused by: java.lang.ClassNotFoundException: net.sf.jasperreports.engine.JRExcep
tion
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 6 more

Sorry if it's dumb question, I'm just beginner.

Thanks for any help!


Solution

  • Java cannot find your main method, because it's not looking in your TestReport class (which is not on the declared classpath)

    You must add the location of your TestReport.class file to your classpath like so:

    java -classpath D:\JasperReports\jasperreports-5.5.0\dist\jasperreports-5.5.0.jar;. TestReport
    

    (note the ; (the windows classpath seperator) and . (your local directory) at the end of your classpath declaration.

    see also this question.