2

In my Spring boot webapp I am trying to implement some backend functionalities to generate PDF files using JasperReports.

I have a file structure looking like this:

File structure

The app, when running, has no problem in seeing the application-*.properties, or any files in static folder.

I have written a piece of code to just generate the report and show it in JasperViewer:

    JasperPrint print = null;
    try {
        JasperCompileManager.compileReportToFile("invoices/invoice.jrxml");
        print = JasperFillManager.fillReport("invoices/invoice.jasper", new HashMap<>());
        JasperViewer jasperViewer = new JasperViewer(print);
        jasperViewer.setVisible(true);
    } catch (JRException e) {
        e.printStackTrace();
    }

But when I'm trying to run the app, all I get is:

net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException: invoices/invoice.jrxml (No such file or directory)
Alex K
  • 22,315
  • 19
  • 108
  • 236
hc0re
  • 1,806
  • 2
  • 26
  • 61

1 Answers1

3

You are using java.io file api operations to load the resources from the classpath. It works when the application in unpacked during development in IDE but will fail when the app is packaged to JAR, hence the FileNotFoundException

Change your code to load the resources from the classpath with InputStream e.g. by using JasperCompileManager.compileReport():.

try (InputStream in = getClass().getResourceAsStream("/invoices/invoice.jrxml")) {
    JasperCompileManager.compileReport(in);
    ...
}
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111