Java jasperReport 文件中发生错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19975718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:59:21  来源:igfitidea点击:

Error Occured in jasperReport File

javajasper-reports

提问by user2865749

First I make one R_D1.jrxml file in iReport 5.1.0.

首先,我在 iReport 5.1.0 中制作了一个 R_D1.jrxml 文件。

My Java code to execute the report looks like:

我执行报告的 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();
  }

}

When I execute the class I get the following exception:

当我执行该类时,出现以下异常:

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

I am not sure what I am doing wrong, or what this exception means.

我不确定我做错了什么,或者这个异常意味着什么。

采纳答案by Jacob Schoen

Your main problem here is that you have not compiled the file. Think of the JRXML file as a Java source file. To run your java file you have to compile it first, and then you can run. The jrxml file is simply the human readable XML file that describes what you want to happen.

您的主要问题是您尚未编译该文件。将 JRXML 文件视为 Java 源文件。要运行您的 java 文件,您必须先编译它,然后才能运行。jrxml 文件只是人类可读的 XML 文件,它描述了您想要发生的事情。

To compile the file you do:

要编译您执行的文件:

JasperCompileManager.compileReport("/home/abcd/report/R_D1.jrxml");

This is going to return you and instance of a JasperReport, which is the compiled file. (this is often written out to a .jasper file, so you do not have to compile the report on each run, but that is beyond the scope of this question). Once you have this you can then fill the report.

这将返回您和 a 的实例JasperReport,它是编译后的文件。(这通常被写入 .jasper 文件,因此您不必在每次运行时编译报告,但这超出了本问题的范围)。一旦你有了这个,你就可以填写报告。

Also, unrelated, but worth mentioning, is that you should be closing the you database connection in a finally block. As in your current example it is never closed, since an exception is thrown. A finally block will ensure that even in the event of an exception it would be closed.

此外,无关但值得一提的是,您应该在 finally 块中关闭数据库连接。在您当前的示例中,它永远不会关闭,因为抛出了异常。finally 块将确保即使发生异常,它也会被关闭。

You sample method should look like:

您的示例方法应如下所示:

public void generateReport() {
  Connection con
  try {
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sentiment","root", "abcd");
    System.out.println("Compiling report...");
    JasperReport jasperReport = JasperCompileManager.compileReport("/home/abcd/report/R_D1.jrxml");
    System.out.println("Filling report...");
    JasperFillManager.fillReportToFile(jasperReport,new HashMap<String, Object> (), con);
    System.out.println("Done!");
  } catch (JRException e) {
    e.printStackTrace();
  } catch (ClassNotFoundException e) {
    e.printStackTrace();
  } catch (SQLException e) {
    e.printStackTrace();
  } finally {
    if (con != null){
      con.close();
    }
  }
}

Hope that helps. Good luck.

希望有帮助。祝你好运。

回答by Krishnamoorthy

If you are creating ".jrxml file" by using ireport tool then which will give you .jasper file ...If you don't want to compile then you can use already compiled .jasper file in your java program like this:

如果您使用 ireport 工具创建“.jrxml 文件”,那么它将为您提供 .jasper 文件......如果您不想编译,那么您可以在您的 Java 程序中使用已经编译的 .jasper 文件,如下所示:

JasperCompileManager.compileReport("/home/abcd/report/R_D1.jasper");

Thanks, Krish

谢谢,克里什