10

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

My Java code to execute the report looks like:

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.

3
  • I also added following jar : commons-beanutils Commented Nov 14, 2013 at 10:53
  • commons-collection-3.1.jar, commons-digester-2.0.jar,commons-logging-1.1.3.jar, jasperreport-5.1.0.jar Commented Nov 14, 2013 at 10:55
  • Please take this as constructive criticism, to help to get help. In my experience the easier it is to read your question, the quicker and the better the help you receive will be. Taking a few extra minutes to make sure you format code blocks, and stack traces in your post is worth it. See stackoverflow.com/editing-help for more help. Commented Nov 14, 2013 at 15:16

2 Answers 2

19

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.

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.

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.

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Should I avoid compiling the report each time I use it?
Ideally, you would compile once, and not everytime you go to use it. It depends on your situation on how you do this. I have worked on apps, where the reports where compiled along with the java code and packaged the jasper files in with the war. But I have also worked on applications that stored the reports in a database and compiled them on first use, and cached the jasper file, so it was only recompiled when it changed. It depends on your application.
2

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:

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

Thanks, Krish

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.