Access the current file path in . jrxml (Jasper Report)

Asked

Viewed 513 times

0

Good afternoon,

I am creating a report in Jasper that contains a sub-report (with Jaspersoft Studio). For this to be loaded, I have to indicate the directory of the sub-report, which is in the same folder as the parent report. I am 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>

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

For the research I did on the Internet, can be used the $P{SUBREPORT_DIR}, but in my case gives me null. I also tried to use the following expression:

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

But to no avail. He can’t detect where he is.

Anyone have any suggestions? Thank you!

1 answer

1

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

I pack my own .jasper inside the application and not out anywhere , in your case on desktop in the report folder. From what I see on the way that this described is not inside a project but outside in a folder.

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

I recommend that you pack the . Search inside your application and stay in the same directory, as the example I posted.

To generate the report use the following method, where the getParametros() returns an object of type Map. Now this configuration if it is distributed to many clients and of course will have different locations I recommend the use of a.properties file which Voce would be able to edit and change its values as it is an external file would be easier to modify, I used to use a file of this in my desktop application to inform whether or not to see 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;
        }   
  • Hello Denis, thank you for your reply. However, my goal would be not to put the directory 'hard coded'. I wish, somehow, there was a way to have a variable that would detect the path where the report is, and then yes, from there, do the " <![CDATA[$P{SUBREPORT_DIR} + "Embarque_relacao_subreport1.Jasper"]]>". This is because it is a client project. On my localhost, the path to the reports folder may be X, but on the client, the path is different, so I wanted to find a dynamic way to detect where the reports are.

  • Voce can do this my dear when Voce inform a Map parameters for example in your case if I wanted to change the default value of the parameter $P{SUBREPORT_DIR} would do as follows: parameters.put("SUBREPORT_DIR","C: Users john Desktop Reports");', edit my answer above to be clearer

  • Remembering that in my case I distribute my application for several machines using the relative patch technique I told you above I never needed to change in anyone’s computer, because the path is not the absolute but the relative

Browser other questions tagged

You are not signed in. Login or sign up in order to post.