首先,我在 iReport 5.1.0 中创建了一个 R_D1.jrxml 文件。
我执行报告的 Java 代码如下所示:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
public class DbReportFill{
Connection con;
public void generateReport() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sentiment","root", "abcd");
System.out.println("Filling report...");
JasperFillManager.fillReportToFile("/home/abcd/report/R_D1.jrxml",new HashMap<String, Object> (), con);
System.out.println("Done!");
con.close();
} catch (JRException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new DbReportFill().generateReport();
}
}
当我执行该类时,出现以下异常:
Filling report...
net.sf.jasperreports.engine.JRException: Error loading object from file : /home/abcd/report/R_D1.jrxml
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:127)
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:99)
at net.sf.jasperreports.engine.JasperFillManager.fillToFile(JasperFillManager.java:117)
at net.sf.jasperreports.engine.JasperFillManager.fillReportToFile(JasperFillManager.java:666)
at DbReportFill.generateReport(DbReportFill.java:24)
at DbReportFill.main(DbReportFill.java:56)
Caused by: java.io.StreamCorruptedException: invalid stream header: 3C3F786D
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:802)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
at net.sf.jasperreports.engine.util.ContextClassLoaderObjectInputStream.<init>(ContextClassLoaderObjectInputStream.java:58)
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:122)
... 5 more
我不确定我做错了什么,或者这个异常意味着什么。