D
D
DDwrt1002019-09-27 15:04:12
Java
DDwrt100, 2019-09-27 15:04:12

How to specify the relative path to the report file in JasperReport?

Good day to all.
Please tell me how to correctly specify the relative path to the report template file in JasperReports.
Now I've encountered a problem that while I'm working with an unpackaged program (I'm running it from ide), the report is being generated.
However, when I build the project, it throws java.io.FileNotFoundException.
The report file gets into Jar during assembly. However, apparently it cannot access the running archive itself.
I form the path to the report file like this

StringBuilder constructReportPlace = new StringBuilder();
constructReportPlace.append(runPath);
constructReportPlace.append("/src/main/resources/reports/Report.jasper");

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sergey, 2019-09-28
@DDwrt100

there are several spsb -
1. regarding the project

System.err.println(
String.format("%s/src/main/resources/%s",
  System.getProperty("user.dir"), resourceFileName));

2. regarding where the jar is located which is executed:
System.err.println(
  String.format("The running application resource path: \"%s\"", 			 
    Thread.currentThread().getContextClassLoader()
      .getResource("").getPath()));

well, there are variations
this.getClass().getClassLoader()
  .getResourceAsStream(

- this is for emnip jar.

D
DDwrt100, 2019-09-30
@DDwrt100

I'm probably doing something wrong, but it doesn't work.

String reportPlace =  String.format("\"%sreports/Task.jasper", 			 
          Thread.currentThread().getContextClassLoader()
            .getResource("").getPath());

      InputStream std = new FileInputStream(reportPlace);

     JasperReport jasperReport = (JasperReport) JRLoader.loadObject(std);

gives 500 error
"status": 500,
  "error": "Internal Server Error",
  "message": " \"file:\\D:\\Java\\Spriп\\target\\Tot.jar!\\BOOT-INF\\classes!\\reports\\Task.jasper (Синтаксическая ошибка в имени файла, имени папки или метке тома)",

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question