制作jar文件后,Jasper报告无效
我已经编写了以下用于创建jasper报告的代码,此代码在NetBeans IDE中正常工作,但在创建该项目的jar文件后,报告无法打开。它也没有显示任何错误。
I have written the following code for creating jasper report, this code working fine in NetBeans IDE, but after creating jar file of that project the report is not opening. Its also not showing any error.
可能是什么问题?
创建jasper报告的代码
Code for creating jasper report
//Path to your .jasper file in your package
String reportSource = "src/report/Allvendor_personal_info.jrxml";
try
{
jasperReport = (JasperReport)
JasperCompileManager.compileReport(reportSource);
jasperPrint = JasperFillManager.fillReport(jasperReport, null, con);
//view report to UI
JasperViewer.viewReport(jasperPrint, false);
con.close();
}
catch(Exception e)
{
JOptionPane.showMessaxgeDialog(null, "Error in genrating report");
}
路径 src
在运行时不存在,你永远不应该引用它。
The path src
will not exist at runtime and you should never reference it.
基于此, Allvendor_personal_info。 jrxml
将是一个嵌入式资源,存储在Jar文件中,你将无法像普通文件那样访问它,相反,你需要使用 Class#getResource
或 Class#getResourceAsStream
Based on this, the Allvendor_personal_info.jrxml
will be an embedded resource, stored within the Jar file, you won't be able to access it like you do normal files, instead, you need to use Class#getResource
or Class#getResourceAsStream
String reportSource = "/report/Allvendor_personal_info.jrxml";
InputStream is = null;
try
{
is = getClass().getResourceAsStream(reportSource);
jasperReport = (JasperReport)JasperCompileManager.compileReport(is);
jasperPrint = JasperFillManager.fillReport(jasperReport, null, con);
//...
} finally {
try {
is.close();
} catch (Exception exp) {
}
}
现在,说到这一点,应该没有理由在运行时编译 .jrxml
文件,相反,你应该在构建时编译这些文件并部署 .jasper
而不是文件。这将改善您的应用程序的性能,因为即使对于基本报告,复杂化过程也不短。
Now, having said that, there should be very little reason to ever compile a .jrxml
file at runtime, instead, you should compile these files at build time and deploy the .jasper
files instead. This will improve the performance of your application as the complication process is not short even for a basic report.
这意味着您将使用...
This would mean you would use...
jasperReport = (JasperReport) JRLoader.loadObjectFromFile(is);
而不是 JasperCompileManager.compileReport