java net.sf.jasperreports.engine.JRException:从文件加载对象时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15250533/
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-10-31 18:59:46  来源:igfitidea点击:

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

javajasper-reports

提问by Abdh-Allah

I am using jasper to display report for my project...Actually i m trying to pass the parameters dynamically to jrxml template as i saw in the link given below but i couldnt.....There seems a problem in my code the error is

我正在使用 jasper 来显示我的项目的报告......实际上我试图将参数动态传递给 jrxml 模板,正如我在下面给出的链接中看到的,但我不能......我的代码中似乎有问题,错误是

        net.sf.jasperreports.engine.JRException: Error loading object from file : C:\Users\db2admin\Documents\NetBeansProjects\ConnectDB\src\sample_report.jrxml
    at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:115)
    at net.sf.jasperreports.engine.JasperFillManager.fillToFile(JasperFillManager.java:487)
    at net.sf.jasperreports.engine.JasperFillManager.fillReportToFile(JasperFillManager.java:874)
    at ConnectDB.main(ConnectDB.java:65)
Caused by: java.io.StreamCorruptedException: invalid stream header: 3C3F786D
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:801)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:298)
    at net.sf.jasperreports.engine.util.ContextClassLoaderObjectInputStream.<init>(ContextClassLoaderObjectInputStream.java:53)
    at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:110)
    ... 3 more

the jasper snippet is

碧玉片段是

ResultSet resultSet = statement.executeQuery("SELECT reg, name FROM students");
JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(resultSet);          

JasperFillManager.fillReportToFile("C:\Users\db2admin\Documents\NetBeansProjects\
             ConnectDB\src\sample_report.jrxml", null, resultSetDataSource);

Finally the xml code is

最后的xml代码是

<?xml version="1.0" encoding="UTF-8"?>
<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="sample_report" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30" uuid="b3b98795-b21f-4006-9295-5ef4248c84ea">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>

    <field name="name" class="java.lang.String"/>
    <field name="reg" class="java.lang.Integer"/>
    <columnHeader>
        <band height="28" splitType="Stretch">
            <staticText>
                <reportElement uuid="6d6bff96-6dcb-4188-bdc0-5c23b2614b05" key="staticText-1" x="40" y="11" width="196" height="15"/>
                <textElement/>
                <text><![CDATA[Name]]>
                </text>
            </staticText>
            <staticText>
                <reportElement uuid="c0267b29-e31b-4afd-a181-86bbcb4fb368" key="staticText-2" x="330" y="11" width="193" height="15"/>
                <textElement/>
                <text><![CDATA[Reg]]>
                </text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="27" splitType="Stretch">
            <textField>
                <reportElement uuid="4a1e6484-9610-421d-b4a4-f61d53458d9e" key="textField" x="47" y="6" width="173" height="18"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{name}]]>
                </textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="01cb3d91-74e7-4594-aca0-605d1aa203a8" key="textField" x="330" y="6" width="100" height="18"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{reg}]]>
                </textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

I referred this site http://www.packtpub.com/article/creating-dynamic-reports-from-databases-jasperreports-3.5

我提到了这个网站 http://www.packtpub.com/article/creating-dynamic-reports-from-databases-jasperreports-3.5

Plz help me with this!!!!

请帮我解决这个问题!!!!

回答by andhdo

If you are working in ireport, review that your subreports are referencing to the .jasper file (not the jrxml).

如果您在 ireport 中工作,请检查您的子报告是否引用了 .jasper 文件(而不是 jrxml)。

If you are publising your report to the jasperreports-server, take in mind that it does a changes in the report definition referencing the jrxml files with a prefix (but not the jasper files).

如果您将报告发布到 jasperreports-server,请记住,它会更改报告定义中引用带有前缀的 jrxml 文件(但不是 jasper 文件)。