Java 带有JasperReport的Spring引导应用程序中的FileNotFoundException
在我的SpringBootWebApp中,我试图实现一些后端功能,使用Java 带有JasperReport的Spring引导应用程序中的FileNotFoundException,java,jasper-reports,filenotfoundexception,Java,Jasper Reports,Filenotfoundexception,在我的SpringBootWebApp中,我试图实现一些后端功能,使用JasperReports生成PDF文件 我的文件结构如下所示: 应用程序在运行时,在查看应用程序-*.properties或静态文件夹中的任何文件时没有问题 我已经编写了一段代码来生成报告并将其显示在JasperViewer中: JasperPrint print = null; try { JasperCompileManager.compileReportToFile("invoices
JasperReports
生成PDF文件
我的文件结构如下所示:
应用程序在运行时,在查看应用程序-*.properties
或静态
文件夹中的任何文件时没有问题
我已经编写了一段代码来生成报告并将其显示在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();
}
您正在使用
java.io
文件api操作从类路径加载资源。当应用程序在IDE中开发期间解包时,它可以工作,但当应用程序打包到JAR时,它将失败,因此出现FileNotFoundException
使用InputStream
更改代码以从类路径加载资源,例如使用:
try(InputStream in=getClass().getResourceAsStream(“/invoices/invoice.jrxml”)){
jaspecompilemanager.compileReport(in);
...
}
成功了,谢谢!
net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException: invoices/invoice.jrxml (No such file or directory)