1

一整天,我一直在尝试基于 JasperReports 库运行我的第一个报告,但我还没有得到我想要的结果。

我有一个ReportTemplate.jrxml文件包含:

<!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>

和我的.java文件:

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();
        }
    }
}

两者都位于同一目录中。所以,我们走吧

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

作品!

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

简单地尝试

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

对不起,如果这是愚蠢的问题,我只是初学者。

谢谢你的帮助!

4

1 回答 1

1

Java 找不到您的 main 方法,因为它没有在您的 TestReport 类中查找(它不在声明的类路径中)

您必须将 TestReport.class 文件的位置添加到类路径中,如下所示:

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

(注意类路径声明末尾的;(Windows 类路径分隔符)和(您的本地目录)。.

另见这个问题

于 2013-10-31T22:12:43.970 回答