JasperReports类路径
我想在我的NetBeans swing项目中包含.jrxml文件。 我使用NetBeans 7.0.1 。 我在源代码包中创建了一个名为“rep”的包,并创建了一个名为“rp.jrxml”的简单.jrxml文件。 我在NetBeans中安装了iReport插件。 当我设置外部.jrxml文件时,会显示(“D:/MyReports/firstreport.jrxml”),但是当我设置NetBeans包路径时,它没有显示。 这是我的代码。
try { String reportSource="/rep/rp.jrxml"; //and also "rep/rp.jrxml" is used.no result. Map params = new HashMap(); JasperReport jasperReport = JasperCompileManager.compileReport(reportSource); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource()); JasperViewer.viewReport(jasperPrint, false); } catch (Exception e) {e.printStackTrace(); }
然后给出以下错误;
net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException: rep\rp.jrxml (The system cannot find the path specified)
如何在我的NetBeans项目中保留jrxml文件并在项目中使用jrxml文件?
这段代码适合我:
public class TestJasper { public static void main(String[] args) { try { String reportSource = "resources/report1.jrxml"; Map params = new HashMap(); JasperReport jasperReport = JasperCompileManager.compileReport(reportSource); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource()); JasperViewer.viewReport(jasperPrint, false); } catch (Exception e) { System.out.println(e.getMessage()); } } }
我的项目结构:
建立 类 TestJasper.class DIST nbproject文件 资源 report1.jrxml SRC TestJasper.java
更新:
为了解决net.sf.jasperreports.engine.JRException: No report compiler set for language : null
问题你可以尝试设置groovy
报告语言并将groovy-all-jdk14
库添加到classpath。
你可以在这里获得groovy库。
语言设置为groovy
的报告标题示例:
这里的解决方案。最好在jar文件本身提供一个绝对路径。同样在运行jar时,使用* .jasper文件最好使用而不是.jrxml,这也可能导致生成报告的速度然后.jasper可以作为输入流发送到JasperFillManager。 这就对了。