JRException Resource not found at when there is a subreport in Jasper Report
Asked Answered
A

3

9

I made a jasper report that includes a subreport using JasperSoft Studio. When i preview it in the studio, it works fine. But when i add it into my java fx project, i have to send the subreport as a parameter to the main report right?

When i do that i keep on getting an error saying that the subreport resource not found. I dont think there is any spelling errors because i hv tried this with almost all the reports that includes subreports.. still keep on getting that error..

These are the things i tried 1. compiled the report again using the jaspersoft studio and includeded them again in the project.. 2. included the jrxml file into the project and compiled it with the Compile Manager.. 3. i read some tutorials and questions saying that to put the expression to the subreport element in the main report as

$P{SUBREPORT_DIR} + "subreport_2.jasper"

but i still keep on getting the same error and i dont know what i am doing wrong Below is my code

method in application used to view the report

@FXML
    public void btnMarkingSchemeClicked(ActionEvent actionEvent) {
        try {

//            JasperReport compiledReport = (JasperReport) JRLoader.loadObject(ViewAssignmentsController.class.getResourceAsStream("/edu/ijse/gdse41/ams/reports/MarkingScheme.jasper"));
            JasperReport compiledReport = JasperCompileManager.compileReport(ViewAssignmentsController.class.getResourceAsStream("/edu/ijse/gdse41/ams/reports/MarkingScheme.jrxml"));
            HashMap<String, Object> parameters = new HashMap<>();
            parameters.put("assignID", aid);
            parameters.put("course", cmbCourse.getSelectionModel().getSelectedItem().toString());
            parameters.put("subject", cmbSub.getSelectionModel().getSelectedItem().toString());
            parameters.put("semester", cmbSem.getSelectionModel().getSelectedItem().toString());
            parameters.put("assign", ctrlAssign.search(aid).getAssignName());
//            JasperReport subReport = (JasperReport) JRLoader.loadObject(ViewAssignmentsController.class.getResourceAsStream("/edu/ijse/gdse41/ams/reports/Answers.jasper"));
            JasperReport subReport = JasperCompileManager.compileReport(ViewAssignmentsController.class.getResourceAsStream("/edu/ijse/gdse41/ams/reports/Answers.jrxml"));
            parameters.put("subReport", subReport);
            JasperPrint fillReport = JasperFillManager.fillReport(compiledReport, parameters, ConnectionFactory.getInstance().getConnection());
            JasperViewer.viewReport(fillReport, false);

        } catch (JRException | SQLException | ClassNotFoundException ex) {
            Logger.getLogger(ViewAssignmentsController.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

my main report source -- MarkingScheme.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1  -->
<!-- 2017-06-02T12:52:20 -->
<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="MarkingScheme" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="c71b946b-2ba2-4208-97f8-f32e8bcf3fb4">
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="amsAdapter"/>
    <parameter name="course" class="java.lang.String"/>
    <parameter name="subject" class="java.lang.String"/>
    <parameter name="semester" class="java.lang.String"/>
    <parameter name="assign" class="java.lang.String"/>
    <parameter name="assignID" class="java.lang.String">
        <defaultValueExpression><![CDATA[$P{assignID}]]></defaultValueExpression>
    </parameter>
    <parameter name="subReport" class="net.sf.jasperreports.engine.JasperReport">
        <parameterDescription><![CDATA[]]></parameterDescription>
        <defaultValueExpression><![CDATA[$P{subReport}]]></defaultValueExpression>
    </parameter>
    <queryString language="SQL">
        <![CDATA[SELECT ams.question.qid,
    ams.question.question,
    ams.assign_ques.aid
FROM ams.assign_ques
    INNER JOIN ams.question ON 
     ams.assign_ques.qid = ams.question.qid and ams.assign_ques.aid=$P{assignID}]]>
    </queryString>
    <field name="qid" class="java.lang.String"/>
    <field name="question" class="java.lang.String"/>
    <field name="aid" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement mode="Opaque" x="0" y="0" width="555" height="79" forecolor="#FFFFFF" backcolor="#113891" uuid="52ed193a-ed11-46ba-a09f-1864b843df96"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font size="29" isBold="true"/>
                </textElement>
                <text><![CDATA[   Marking Scheme]]></text>
            </staticText>
        </band>
    </title>
    <pageHeader>
        <band height="80" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="80" height="40" uuid="55b35bb0-5ea8-4417-88be-0083cfee5037"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font size="14"/>
                </textElement>
                <text><![CDATA[Course]]></text>
            </staticText>
            <staticText>
                <reportElement x="0" y="40" width="80" height="40" uuid="1dc04c2d-9cde-4905-9650-03cc96d406e3"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font size="14"/>
                </textElement>
                <text><![CDATA[Semester]]></text>
            </staticText>
            <staticText>
                <reportElement x="260" y="0" width="100" height="40" uuid="4de2dbe2-bb88-4dbf-88d7-16b36bba8fb4"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font size="14"/>
                </textElement>
                <text><![CDATA[Subject]]></text>
            </staticText>
            <staticText>
                <reportElement x="260" y="40" width="100" height="40" uuid="bb91c5d3-4cde-45eb-8083-012fe6183396"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font size="14"/>
                </textElement>
                <text><![CDATA[Assignment]]></text>
            </staticText>
            <textField>
                <reportElement x="90" y="0" width="160" height="40" forecolor="#123278" uuid="8016c655-1bea-480a-9179-3da4e1e930ad"/>
                <textElement verticalAlignment="Middle">
                    <font size="13"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{course}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="370" y="0" width="170" height="40" forecolor="#123278" uuid="23e58b4b-0bf1-41ae-89f0-48808a0abdd9"/>
                <textElement verticalAlignment="Middle">
                    <font size="13"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{subject}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="90" y="40" width="160" height="40" forecolor="#123278" uuid="650c412a-0c42-4dea-a96f-35d03c11c926"/>
                <textElement verticalAlignment="Middle">
                    <font size="13"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{semester}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="370" y="40" width="170" height="40" forecolor="#123278" uuid="c8cbea5b-afb3-4c85-a96a-156d1c82fb50"/>
                <textElement verticalAlignment="Middle">
                    <font size="13"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{assign}]]></textFieldExpression>
            </textField>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="30" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="100" height="30" uuid="f7b84a13-8e80-4c9c-8888-3b081f9d0f55"/>
                <textElement>
                    <font size="16" isBold="true"/>
                </textElement>
                <text><![CDATA[Questions]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="41" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="80" height="20" uuid="9f05a7d2-ac5f-4412-b26b-ab37033cc0fe"/>
                <textElement verticalAlignment="Middle">
                    <font size="12"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{qid}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="100" y="0" width="440" height="20" uuid="a299bfe0-a0d1-4aae-8f75-5b58d24b1760"/>
                <textElement verticalAlignment="Middle">
                    <font size="12"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{question}]]></textFieldExpression>
            </textField>
            <subreport>
                <reportElement x="100" y="20" width="440" height="20" uuid="72f8c7c2-82ca-48d3-bb89-d5bb4846e1bc"/>
                <subreportParameter name="qid">
                    <subreportParameterExpression><![CDATA[$F{qid}]]></subreportParameterExpression>
                </subreportParameter>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <subreportExpression><![CDATA["Answers.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </detail>
</jasperReport>

my sub report source -- Answers.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1  -->
<!-- 2017-06-02T12:53:52 -->
<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="Answers" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="142adbc1-da50-458b-b076-ba8aa716124f">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="amsAdapter"/>
    <property name="com.jaspersoft.studio.data.sql.tables" value="YW1zLmFuc3dlciAsMTUsMTUsOGE1MjQ1NzQtOGY1ZS00ZWY4LWFiMDAtOWVjYmVmNmU0MTFjOw=="/>
    <parameter name="qid" class="java.lang.String">
        <defaultValueExpression><![CDATA[$P{qid}]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[SELECT ams.answer.answer_id,
    ams.answer.answer,
    ams.answer.correctness,
    ams.answer.qid
FROM ams.answer where ams.answer.qid=$P{qid} and ams.answer.correctness=1]]>
    </queryString>
    <field name="answer_id" class="java.lang.String"/>
    <field name="answer" class="java.lang.String"/>
    <field name="correctness" class="java.lang.Boolean"/>
    <field name="qid" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="80" height="20" uuid="517ba9e8-6637-4808-8517-1485d4125971"/>
                <textElement>
                    <font size="12"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{answer_id}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="90" y="0" width="460" height="20" uuid="4384510c-5dea-47a0-b24b-d4b3bd4088fc"/>
                <textElement>
                    <font size="12"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{answer}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

the error im getting

net.sf.jasperreports.engine.JRException: Resource not found at: Answers.jasper. at net.sf.jasperreports.repo.RepositoryUtil.getResourceFromLocation(RepositoryUtil.java:153) at net.sf.jasperreports.repo.RepositoryUtil.getReport(RepositoryUtil.java:112) at net.sf.jasperreports.engine.fill.JRFillSubreport.loadReport(JRFillSubreport.java:398) at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateReport(JRFillSubreport.java:365) at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateSubreport(JRFillSubreport.java:427) at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluate(JRFillSubreport.java:341) at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:381) at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:500) at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2022) at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:748) at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:255) at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:115) at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:582) at net.sf.jasperreports.engine.fill.BaseReportFiller.fill(BaseReportFiller.java:396) at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:90) at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:456) at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:863) at edu.ijse.gdse41.ams.view.controller.ViewAssignmentsController.btnMarkingSchemeClicked(ViewAssignmentsController.java:174) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71) at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275) at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769) at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) at javafx.event.Event.fireEvent(Event.java:198) at javafx.scene.Node.fireEvent(Node.java:8413) at javafx.scene.control.Button.fire(Button.java:185) at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182) at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96) at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89) at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) at javafx.event.Event.fireEvent(Event.java:198) at javafx.scene.Scene$MouseHandler.process(Scene.java:3757) at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485) at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762) at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494) at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381) at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417) at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389) at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416) at com.sun.glass.ui.View.handleMouseEvent(View.java:555) at com.sun.glass.ui.View.notifyMouse(View.java:937) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) at java.lang.Thread.run(Thread.java:745)

Aschim answered 2/6, 2017 at 7:27 Comment(0)
T
6

It is because even though you have created a parameter called "subReport" it hasn't been used anywhere.

<subreport>
    <reportElement x="100" y="20" width="440" height="20" uuid="72f8c7c2-82ca-48d3-bb89-d5bb4846e1bc"/>
    <subreportParameter name="qid">
        <subreportParameterExpression><![CDATA[$F{qid}]]></subreportParameterExpression>
    </subreportParameter>
    <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
    <subreportExpression><![CDATA["Answers.jasper"]]></subreportExpression>
</subreport>

See? It is still "Answers.jasper" there in the main report. That is why it works fine in Jasper Studio. Let's change it to,

<subreport>
    <reportElement x="100" y="20" width="440" height="20" uuid="72f8c7c2-82ca-48d3-bb89-d5bb4846e1bc"/>
    <subreportParameter name="qid">
        <subreportParameterExpression><![CDATA[$F{qid}]]></subreportParameterExpression>
    </subreportParameter>
    <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
    <subreportExpression><![CDATA[$P{subReport}]]></subreportExpression>
</subreport>

I am pretty sure that I am not the one who taught you to use Jasper :) Let's hope it will work

Telluride answered 2/6, 2017 at 11:7 Comment(2)
I'm having this issue, but the difference is that I'm not programming the form, I'm designing it with Jaspersoft Studio. What can I do?Nominative
Thanks!!! just a link for any who is using the .jasper file and not: InputStream inSR = getClass().getResourceAsStream("/reports/MyReports/productividadResumen.jasper"); ... JasperReport subReport = (JasperReport)JRLoader.loadObject(inSR); parameters.put("subReport1", subReport); Hope it hepls!!!Pine
U
4

In MarkingScheme.jrxml change from

<subreportExpression><![CDATA["Answers.jasper"]]></subreportExpression>

to

<subreportExpression><![CDATA[$P{subReport}]]></subreportExpression>

Change the class adding the Answers.jasper including its path.

parameters.put("subReport", "/edu/ijse/gdse41/ams/reports/Answers.jasper");

The path must be completed starting from the root context (/).

Udo answered 19/2, 2018 at 18:10 Comment(1)
The path you gave is static. It should dynamically read from the context pathHerries
H
4

Your subreportExpression tag should expect $P{SUBREPORT_DIR} + "Answer.jasper" like this:

<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "Answer.jasper"]]></subreportExpression>

And you need to provide the path dynamically from your context for $P{SUBREPORT_DIR}

I am using Groovy on Grails! Basically, the report misses the value of parameter for SUBREPORT_DIR in your .jrxml. My reports and subReports are in web-app/reports folder. So, while generating report with subReport, I added a path value like the following:

params.put('SUBREPORT_DIR', getReportDirectory() + File.separator)

where getReportDirectory() function looks like the following:

def getReportDirectory() {
    String REPORT_DIRECTORY = null;
    if (!REPORT_DIRECTORY) {
        File reportFolder = ApplicationHolder.application.parentContext.getResource('/reports').file;
        REPORT_DIRECTORY = reportFolder.absolutePath;
    }

   return REPORT_DIRECTORY;
}
Herries answered 13/6, 2021 at 5:56 Comment(2)
Helpful answer indeedOxide
When using .jasper as extension, I get "Error loading object from InputStream"Organotherapy

© 2022 - 2024 — McMap. All rights reserved.