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);
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.
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);
}
Having said all that, you should not be deploying or compiling your .jrxml
files at runtime.
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, you can also use...
JasperReport report = (JasperReport)JRLoader.loadObject(getClass().getResource("report.jrxml"));