制作jar文件后,Jasper报告无效 [英] Jasper report is not working after making jar file

查看:130
本文介绍了制作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

这篇关于制作jar文件后,Jasper报告无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆