无法从Jar文件中调用jasper报告 [英] Unable to Call jasper report from Jar file
问题描述
我使用以下代码调用jasper报告
I am using the following code to call a jasper report
String reportSource = "/report.jrxml";
InputStream is = getClass().getResourceAsStream(reportSource);
JasperReport jasperReport = (JasperReport) JasperCompileManager.compileReport(is);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, conn);
JasperViewer.viewReport(jasperPrint);
当我在netbeans ide中运行代码时,代码工作正常。但是当我构建应用程序并创建jar并运行它时,我没有获得报告弹出窗口。
Code is working fine when i run it in netbeans ide. but when i build the application and create the jar and run it , i am not getting report popup.
推荐答案
不要转换 getClass()的结果.getResource(report.jrxml)
到字符串
,而是要使用 getClass()。getResourceAsStream(report.jrxml)
并将其传递给 JasperCompileManager.compileReport(InputStream)
Don't convert the results of getClass().getResource("report.jrxml")
to a String
, instead, you want to use getClass().getResourceAsStream("report.jrxml")
and pass this into JasperCompileManager.compileReport(InputStream)
try (InputStream is = getClass().getResourceAsStream("report.jrxml")) {
JasperReport jr = JasperCompileManager.compileReport(is);
}
说了这么多,你不应该部署或编译你的 .jrxml
运行时的文件。
Having said all that, you should not be deploying or compiling your .jrxml
files at runtime.
作为构建过程的一部分,您应该编译。 jrxml
文件到 .japser
文件,只需在运行时加载和填充它们。
As part of your build process, you should be compiling the .jrxml
files into .japser
files and simply loading and filling them at runtime.
try (InputStream is = getClass().getResourceAsStream("report.jrxml")) {
JasperReport report = (JasperReport)JRLoader.loadObject(is);
}
这样你就省去了每次浪费运行时编译报告的麻烦。 ..
This way you save yourself the hassle of wasting runtime compiling the report each time...
ps,你也可以使用......
ps, you can also use...
JasperReport report = (JasperReport)JRLoader.loadObject(getClass().getResource("report.jrxml"));
这篇关于无法从Jar文件中调用jasper报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!