0
votes

I have a problem trying to load a .jasper subReport that is inside a .war file.
The idea is to load a subReport that is inside "parte1" folder.

my .war file

In documentation says that you can't pass relative path to load a subReport file ...

You can't use a relative path to locate the subreport file; that is if you have a report in c:\myreport\main_report.jasper, you cannot refer to a subreport using an expression like ..\mysubreports\mysubreport.jasper.

This is because JasperReports does not keep in memory the original location of the Jasper file that it’s working with. This makes perfect sense, considering that a Jasper object is not necessarily loaded from a physical file.

So I'm trying to pass a full path to the .jasper file.
1.- I'm getting the context using @Autowired and then completing the path as a String...

parameters.put("SUBREPORT_DIR", contextPath + "WEB-INF" + File.separator + "report" + File.separator + "parte1" + File.separator);

2.- Pass the path to jasperReport as a parameter

printFileName = JasperFillManager.fillReportToFile(getPath(sourceFileName), parameters, new JRBeanCollectionDataSource(lstDataSource));

3.- give the path to load the subReport file

<subreport>
            <reportElement x="50" y="50" width="200" height="200" uuid="ced7076b-b6ba-4973-8e4c-cd289bd77c4e"/>
            <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{motivoReclamo})]]></dataSourceExpression>
            <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "ClsMotivoReclamoReportBean.jasper"]]></subreportExpression>
        </subreport>

but, when try to run it. It displays this error message ...

2019-01-15 05:29:19 [http-nio-8081-exec-2] ERROR xxx.xxx.xx.report.PDFGenerator - net.sf.jasperreports.engine.JRException: Resource not found at: C:\Program Files\Apache Software Foundation\Tomcat 8.5\webapps\xxxxxxxxx\WEB-INF\report\parte1\ClsMotivoReclamoReportBean.jasper.

Thanks in advance.

enter image description here

UPDATE

I'm using Spring Boot and generating a .war file

I'm using this maven plugin to compile the reports and generate the .jasper file.
https://github.com/alexnederlof/Jasper-report-maven-plugin
Then I use this maven plugin to copy some images needed in the report ...

<plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/generated-sources/report</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${project.basedir}/src/main/jasperfiles</directory>
                                <filtering>true</filtering>
                                <excludes>
                                    <exclude>**/*.jrxml</exclude>
                                </excludes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

And then I use this maven plugin to copy the jasper files into .war

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <webResources>
                    <resource>
                        <directory>${project.build.directory}/generated-sources/report</directory>
                        <targetPath>WEB-INF/classes/report</targetPath>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
1

1 Answers

0
votes

You can alternatively define the sub report as InputStream:

In your JRXML:

<parameter name="MY_SUB_REPORT" class="java.io.InputStream" isForPrompting="false"/>
  ...
</subreport>
  ...
  <subreportExpression><![CDATA[$P{MY_SUB_REPORT}]]></subreportExpression>
</subreport>

In your Java Class:

parameters.put("MY_SUB_REPORT", new ByteArrayInputStream(Files.readAllBytes(path)));