我整天都试图根据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
对不起,如果这是一个愚蠢的问题,我只是初学者。
感谢您的帮助!
答案 0 :(得分:0)
Java找不到你的main方法,因为它没有查找你的TestReport类(它不在声明的类路径上)
您必须将TestReport.class文件的位置添加到类路径中,如下所示:
java -classpath D:\JasperReports\jasperreports-5.5.0\dist\jasperreports-5.5.0.jar;. TestReport
(请注意类路径声明末尾的;
(Windows类路径分隔符)和.
(您的本地目录)。