Jasper:从文件加载对象时出错

时间:2014-08-10 19:07:31

标签: java jar

首次使用iReport。我添加了所有必需的JAR文件,但不断收到相同的错误。该怎么办?错误消息如下:

Exceptionnet.sf.jasperreports.engine.JRException: Error loading object from file

导致错误的代码:

    private void btnReportActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            String jrxmlFile = "C:\\Users\\Heart\\Desktop\\report1.jrxml";
            Connection con = DriverManager.getConnection("jdbc:mysql:///database","root","");

            JasperPrint print = (JasperPrint) JasperFillManager.fillReport(jrxmlFile, null, con);
            JasperViewer.viewReport(jprint);
        } catch (Exception e) {
            System.out.print("Exception" + e);
           // e.printStackTrace();
        }
    } 

堆栈跟踪

    net.sf.jasperreports.engine.JRException: Error loading object from file : C:\Users\Heart\Desktop\report1.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.fill(JasperFillManager.java:272) at 
    net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:‌​745) at 
    TableExaminee.btnReportActionPerformed(TableExaminee.java:384)

1 个答案:

答案 0 :(得分:0)

尝试以下代码,

 String jrxmlFile = "C:\\Users\\Heart\\Desktop\\report1.jrxml";
Connection con = DriverManager.getConnection("jdbc:mysql:///database","root","");
 JasperReport jasperReport = JasperCompileManager.compileReport(jrxmlFile);
JasperPrint print = (JasperPrint) JasperFillManager.fillReportToFile(jrxmlFile, new         HashMap<String, Object> (), con);
JasperViewer.viewReport(jprint);

有关详细信息,请访问此link