1

I'm creating a small web app, that will generate and generate a pdf file. The app is created with SpringBoot 2.5.4.

My first intent use this line to compile the jasper report

JasperReport compileReport = JasperCompileManager.compileReport(new FileInputStream("src/main/resources/Invoice.jrxml"));

Everything works fine within the IDE (STS 4.x), but when I "build" and run with java -jar, I get a FileNotFoundException.

Then I decided to compile the .jrxml and use .jasper file. And also I want to move my files to /resources/static/reports folder, to allow the package to include that files.

But again I'm stuck, I want to retrieve static files within SpringBoot app, and when I run it, no matter how... I could load and read that file.

What I'm doing wrong?

Best Regards

Nicolas400
  • 563
  • 1
  • 7
  • 29

1 Answers1

1

A FileInputStream reads input bytes from a file in a file system – it won't let you access a file that is contained in a compressed JAR. Instead you could do the following:

getClass().getResourceAsStream("/Invoice.jrxml");

This should work if your file is contained in src/main/resources.

slauth
  • 2,667
  • 1
  • 10
  • 18
  • Thanks! I think my error was that I created a folder "reports" inside static folder, and the correct place was in "resources". The with lines i got it... String reportFilename = "/reports/invoice.jasper"; InputStream is = getClass().getResourceAsStream(reportFilename); System.out.println(is.toString()); JasperReport jcReport = (JasperReport) JRLoader.loadObject(is); – Nicolas400 Sep 06 '21 at 12:51