0

In my project, i am using jasper report, i have write code to launch jasper report by JButton "Print customer" , but there is lots of errors, The errors is

Errors

net.sf.jasperreports.engine.JRException: Error loading object from file : res\customer.jrxml at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:131) at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:103) at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:307) at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:772) at view.CustomerReposrt$2.mouseClicked(CustomerReposrt.java:63) at java.desktop/java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:278) at java.desktop/java.awt.Component.processMouseEvent(Component.java:6617) at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342) at java.desktop/java.awt.Component.processEvent(Component.java:6379) at java.desktop/java.awt.Container.processEvent(Container.java:2263) at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4990) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822) at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4919) at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4557) at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4489) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307) at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2769) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822) at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715) at java.base/java.security.AccessController.doPrivileged(AccessController.java:391) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95) at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745) at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743) at java.base/java.security.AccessController.doPrivileged(AccessController.java:391) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85) at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742) at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203) at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124) at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90) Caused by: java.io.StreamCorruptedException: invalid stream header: 3C3F786D at java.base/java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:966) at java.base/java.io.ObjectInputStream.<init>(ObjectInputStream.java:405) at net.sf.jasperreports.engine.util.ContextClassLoaderObjectInputStream.<init>(ContextClassLoaderObjectInputStream.java:58) at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:126) ... 35 more 

Jasper Report code

<?xml version="1.0" encoding="UTF-8"?> <!-- Created with Jaspersoft Studio version 6.16.0.final using JasperReports Library version 6.16.0-48579d909b7943b64690c65c71e07e0b80981928 --> <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="customer" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="dcd25edb-27be-46bd-aea7-8bb63a227644"> <property name="com.jaspersoft.studio.data.sql.tables" value=""/> <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Customer Adapter "/> <queryString language="SQL"> <![CDATA[select * from customer]]> </queryString> <field name="name" class="java.lang.String"> <property name="com.jaspersoft.studio.field.name" value="name"/> <property name="com.jaspersoft.studio.field.label" value="name"/> <property name="com.jaspersoft.studio.field.tree.path" value="customer"/> <fieldDescription><![CDATA[]]></fieldDescription> </field> <field name="address" class="java.lang.String"> <property name="com.jaspersoft.studio.field.name" value="address"/> <property name="com.jaspersoft.studio.field.label" value="address"/> <property name="com.jaspersoft.studio.field.tree.path" value="customer"/> <fieldDescription><![CDATA[]]></fieldDescription> </field> <variable name="name1" class="java.lang.Integer" resetType="Column" calculation="Count"> <variableExpression><![CDATA[$F{name}]]></variableExpression> </variable> <background> <band splitType="Stretch"/> </background> <title> <band height="79" splitType="Stretch"> <staticText> <reportElement x="216" y="20" width="151" height="41" uuid="c26299f7-0288-4284-83fd-a68b6fd60c33"/> <textElement> <font fontName="Andalus" size="20" isBold="true"/> </textElement> <text><![CDATA[Customer Test]]></text> </staticText> </band> </title> <pageHeader> <band height="35" splitType="Stretch"/> </pageHeader> <columnHeader> <band height="61" splitType="Stretch"> <staticText> <reportElement x="220" y="10" width="100" height="30" uuid="0b82e110-dabd-43fe-b43b-9fd52e97b839"> <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="eab7558a-fdc0-4ae6-9673-8156f36eeed0"/> </reportElement> <text><![CDATA[name]]></text> </staticText> <staticText> <reportElement x="438" y="0" width="100" height="30" uuid="e03c8d1e-b9b1-49cd-81a6-76dcefeab68e"> <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="cedacd22-bfc0-414b-aaf0-5fec63b4b4e2"/> </reportElement> <text><![CDATA[address]]></text> </staticText> <line> <reportElement x="-10" y="40" width="585" height="7" uuid="dd22e933-1ea2-4cd7-9730-61daaf0dcad9"> <property name="com.jaspersoft.studio.unit.height" value="px"/> </reportElement> </line> </band> </columnHeader> <detail> <band height="125" splitType="Stretch"> <textField> <reportElement x="10" y="20" width="100" height="30" uuid="8bd6e1b0-5f10-4b95-990b-0ee685ec2481"/> <textFieldExpression><![CDATA[$V{name1}]]></textFieldExpression> </textField> <textField> <reportElement x="200" y="30" width="100" height="30" uuid="2c43145d-534c-4a94-af83-627a1a2f52bd"> <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="eab7558a-fdc0-4ae6-9673-8156f36eeed0"/> </reportElement> <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression> </textField> <textField> <reportElement x="438" y="18" width="100" height="30" uuid="3e7b8e0c-ce9c-40c8-b94d-868d34fe4e28"> <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="cedacd22-bfc0-414b-aaf0-5fec63b4b4e2"/> </reportElement> <textFieldExpression><![CDATA[$F{address}]]></textFieldExpression> </textField> </band> </detail> <columnFooter> <band height="45" splitType="Stretch"/> </columnFooter> <pageFooter> <band height="54" splitType="Stretch"/> </pageFooter> <summary> <band height="42" splitType="Stretch"/> </summary> </jasperReport> 

Java Class Where JButton

package view; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import org.apache.commons.collections.map.HashedMap; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.view.JasperViewer; import util.MysqlHelper; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Font; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Map; public class CustomerReposrt extends JFrame { private JPanel contentPane; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { CustomerReposrt frame = new CustomerReposrt(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public CustomerReposrt() { setTitle("Customer report"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 610, 484); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JButton btnNewButton = new JButton("Print customer"); btnNewButton.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { String report="res\\customer.jrxml"; Map map=new HashedMap(); try { JasperPrint jp=JasperFillManager.fillReport(report, map,MysqlHelper.openDb()); JasperViewer jv=new JasperViewer(jp,false); jv.setVisible(true); } catch (ClassNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (JRException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 18)); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); btnNewButton.setBounds(220, 27, 201, 36); contentPane.add(btnNewButton); } } 

This is my Dadabase connection Class

package util; import java.sql.*; public class MysqlHelper { private static String driver="com.mysql.jdbc.Driver"; private static String url="jdbc:mysql://localhost/customerdb"; private static java.sql.Connection connection=null; public static Connection openDb() throws ClassNotFoundException { try { Class.forName(driver); connection=DriverManager.getConnection(url,"root","root"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return connection; } public static void closeDb() { if (connection!=null) { try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } 

Main Code for LAUNCHING REPORT

String report="res\\customer.jrxml"; Map map=new HashedMap(); try { JasperPrint jp=JasperFillManager.fillReport(report, map,MysqlHelper.openDb()); JasperViewer jv=new JasperViewer(jp,false); jv.setVisible(true); } catch (ClassNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (JRException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } 

Also I have uploded the project on github, Click here*

4
  • how you call the jasper is irrelevant, whether it is from Swing or another framework, this is a Jasper issue. Commented May 2, 2021 at 8:08
  • 1
    This question is about the same error (though the code differs a bit). Look at how he builds his paths. There's also no need to pass an empty HashedMap, null should do just fine: stackoverflow.com/questions/56731529/… Commented May 2, 2021 at 8:14
  • This 'Error loading object from file : res\customer.jrxml' hints that there's something wrong with that file, or either the link to it. Commented May 2, 2021 at 8:16
  • 1
    This is quote from javadoc for method you are trying to use: @param sourceFileName source file containing the compiled report design. You can always perform a debug or at least view the documentation or even source code. JasperFillManager Commented May 2, 2021 at 8:28

0