【问题标题】:FileNotFoundException in a Spring Boot application with JasperReport [duplicate]带有 JasperReport 的 Spring Boot 应用程序中的 FileNotFoundException [重复]
【发布时间】:2019-03-22 02:46:52
【问题描述】:

在我的 Spring boot webapp 中,我尝试实现一些后端功能以使用 JasperReports 生成 PDF 文件。

我的文件结构如下:

应用程序在运行时可以毫无问题地查看application-*.propertiesstatic 文件夹中的任何文件。

我已经编写了一段代码来生成报告并显示在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();
    }

但是当我尝试运行应用程序时,我得到的只是:

net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException: invoices/invoice.jrxml (No such file or directory)

【问题讨论】:

    标签: java jasper-reports filenotfoundexception


    【解决方案1】:

    您正在使用 java.io 文件 api 操作从类路径加载资源。它在应用程序在 IDE 开发期间解包时工作,但在应用程序打包到 JAR 时会失败,因此 FileNotFoundException

    更改代码以使用 InputStream 从类路径加载资源,例如通过使用JasperCompileManager.compileReport():.

    try (InputStream in = getClass().getResourceAsStream("/invoices/invoice.jrxml")) {
        JasperCompileManager.compileReport(in);
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-07
      • 2018-12-05
      • 2015-03-28
      • 1970-01-01
      • 2019-08-12
      • 2016-06-13
      • 2016-04-08
      • 2021-10-20
      • 2019-05-12
      相关资源
      最近更新 更多