Access the location of the current file in .jrxml (Jasper Report)

0

Good afternoon,

I'm creating a report in Jasper that contains a subreport (with Jaspersoft Studio). In order for it to be loaded, I have to indicate the subreport directory, which is in the same folder as the parent report. I'm currently using the following method in the parent report:

        <subreport>
            <reportElement x="1" y="65" width="250" height="25" uuid="330b01bb-6d94-4644-b718-0fe59f69ce93"/>
            <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
            <subreportExpression><![CDATA["C:\Users\john\Desktop\reports\subreport_test.jasper"]]></subreportExpression>
        </subreport>

In this way, it can load. It works! However, I wanted it to be dynamic: that is, the subreportExpression would detect directly where it is, and thus would not have to enter the 'hard-coded' location, as in the above code.

By searching the Internet, you can use $ P {SUBREPORT_DIR}, but in my case give me null. I also tried using the following expression:

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

But to no avail. He can not sense where he is.

Does anyone have any suggestions? Thanks!

    
asked by anonymous 11.12.2017 / 19:50

1 answer

1

I use the relative path that refers to my classpath within a parameter as it can be seen below in my parameter $P{SUBREPORT_DIR} is a string parameter that receives the following value as default: "br/com/blogspot/denisbenjamim/iReport/" remembering that the br is a folder inside the src (classpath) directory, the reports are inside the iReport folder.

I pack my .jasper within the application and not out anywhere, in your case on the desktop in the report folder. From what I see in the path that is described is not inside a project but it was in a folder.

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

I recommend that you package the .jasper within your application and be in the same directory as the example I posted.

To generate the report I use the following method, where getParametros() returns an object of type Map . Now this configuration if it is distributed to many clients and of course will have different sites I recommend using a .properties file which you would be able to edit and change the values of it as it is an external file would be easier to modify, I used to use one file in my desktop application to tell you whether or not you want to see the Hibernate sql parameters

    private JasperPrint geradorRelatorio() {
            try {
                fileVirtualizer.cleanup();
                getParametros().put(JRParameter.REPORT_VIRTUALIZER, fileVirtualizer);
                getParametros().put("SUBREPORT_DIR", "Aqui voce iria settar o caminho no seu cliente para o diretorio onde estaria o .jasper");
                if (conexao != null && dataSource == null) {
                    return JasperFillManager.fillReport(getStream(), getParametros(), getConecta());
                } else {
                    return JasperFillManager.fillReport(getStream(), getParametros(), getDataSource());
                }
            } catch (JRException |Relatorio_Exception  ex) {
              ex.printStackTrace();
            } 
            return null;
        }   
    
12.12.2017 / 14:13