尝试从Eclipse执行JasperReports 6.3.1简单报告:
package test.jaspertest;
import java.util.HashMap;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
public class JasperTest {
public static void main(String[] args) {
System.out.println("Start");
try {
JasperReport jasperReport = JasperCompileManager.compileReport("reports/simple.xml");
JasperPrint jasperPrint = JasperFillManager.fillReport(
jasperReport,
new HashMap(),
new JREmptyDataSource());
JasperExportManager.exportReportToPdfFile(jasperPrint, "reports/simple.pdf");
} catch (JRException e) {
e.printStackTrace();
}
}
}
有这个消息:
Apr 11, 2017 1:59:41 PM net.sf.jasperreports.extensions.DefaultExtensionsRegistry loadRegistries
WARNING: Extension resource jar:file:/C:/Portable%20Programs/eclipse/packages/jasperreports/dist/jasperreports-javaflow-6.3.1.jar!/jasperreports_extension.properties was found to be a duplicate of jar:file:/C:/Portable%20Programs/eclipse/packages/jasperreports/dist/jasperreports-6.3.1.jar!/jasperreports_extension.properties in classloader sun.misc.Launcher$AppClassLoader@1d16e93
Start
Exception in thread "main" java.lang.NoClassDefFoundError: com/lowagie/text/DocumentException
at net.sf.jasperreports.engine.JasperExportManager.exportToPdfFile(JasperExportManager.java:150)
at net.sf.jasperreports.engine.JasperExportManager.exportReportToPdfFile(JasperExportManager.java:503)
at test.jaspertest.JasperTest.main(JasperTest.java:24)
Caused by: java.lang.ClassNotFoundException: com.lowagie.text.DocumentException
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more
Java版本1.8.0.121。我对Java不是很熟悉,所以有人可以帮我解决这个问题吗?提前谢谢!
答案 0 :(得分:2)
您错过了类路径中的itext-2.1.7.jar
。添加它,错误将消失。
itext.jar
是jarperReport