NetBeans:JasperReport Exception

2019-02-18 11:50发布

问题:

I'm working on a project that needs Jasper reporting, I have used the code fragment mentioned below to view the report in NetBeans 6.1 (the report is originally generated and compiled using iReport 3.6.0),my requirement is to print this report using a simple Swing application.

Code fragment:

    public class JasperCheck {

    public static void main(String[] args) {
        String reportSource = "E:/Projects/report.jrxml";
        String reportDest = "E:/Projects/report.html";

        Map<String, Object> params = new HashMap<String, Object>();
        try {

            JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);

            JasperPrint jasperPrint =
                    JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource());

            JasperExportManager.exportReportToHtmlFile(jasperPrint, reportDest);

            JasperViewer.viewReport(jasperPrint);
        } catch (JRException ex) {
            System.out.println(ex);
        }
    }
}

But when I run this it gives an exception that I couldn't figure out.

Exception:
Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/groovy/control/CompilationFailedException
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:247)
        at net.sf.jasperreports.engine.util.JRClassLoader.loadClassForRealName(JRClassLoader.java:157)
        at net.sf.jasperreports.engine.util.JRClassLoader.loadClassForName(JRClassLoader.java:115)
        at net.sf.jasperreports.engine.JasperCompileManager.getCompiler(JasperCompileManager.java:511)
        at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:215)
        at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:148)
        at src.JasperCheck.main(JasperCheck.java:31)
Caused by: java.lang.ClassNotFoundException: org.codehaus.groovy.control.CompilationFailedException
        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 java.lang.ClassLoader.loadClass(ClassLoader.java:252)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
        ... 8 more
Java Result: 1

Thanks.

回答1:

Try:

JasperExportManager.exportReportToHtmlFile(jasperPrint, reportDest);

See JasperExportManager.exportReportToHtmlFile(). I assume that's the method you mean.



回答2:

It means that the groovy library dependency is missing.

We can add the groovy-1.7.5.jar (it depends on JasperReports version) in classpath to resolve this issue.

We can find the groovy's dependency in the the JasperReports library pom.xml file. For the JR 4.5.0 version it is:

<dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>1.7.5</version>
        <scope>compile</scope>
        <optional>true</optional>
</dependency>

In case of using maven we can add this dependency to the project:

<dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>1.7.5</version>
</dependency>


回答3:

This is because, while creating a report you have selected language as Grrovy but this using jrxml in java program.In jrxml file,