本文介绍了JasperReports类路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 NetBeans swing项目中包含 .jrxml 文件。我使用 NetBeans 7.0.1 。我在源包中创建了一个名为rep的包,并创建了一个名为rp.jrxml的简单 .jrxml 文件。
我在 NetBeans 中安装了 iReport 插件。当我设置外部 .jrxml 文件时,会显示(D:/MyReports/firstreport.jrxml),但是当我设置 NetBeans 包路径时,它不是所示。这是我的代码。

I want to include the .jrxml file in my NetBeans swing project. I use NetBeans 7.0.1. I created a package inside source package called "rep" and created a simple .jrxml file called "rp.jrxml".I have installed the iReport plugin in NetBeans. When I set a outer .jrxml file, it is showed ("D:/MyReports/firstreport.jrxml") but when I set the NetBeans package path, it was not shown. Here is my code.

try {
       String reportSource="/rep/rp.jrxml";  //and also "rep/rp.jrxml" is used.no result.
        Map<String, Object> params = new HashMap<String, Object>();
        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)

如何将 jrxml 文件保存在我的 NetBeans 项目并在项目中使用 jrxml 文件?

How I can keep jrxml files inside my NetBeans project and use jrxml files inside the project?

推荐答案

此代码适合我:

public class TestJasper {

    public static void main(String[] args) {
        try {
            String reportSource = "resources/report1.jrxml";
            Map<String, Object> params = new HashMap<String, Object>();
            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());
        }
    }
}

我的项目结构:


build
    classes
        TestJasper.class
dist
nbproject
resources
    report1.jrxml
src
    TestJasper.java

更新:

用于解决 net.sf.jasperreports.engine.JRException:没有为语言设置报告编译器:null 问题你可以尝试设置 groovy 报告语言并将 groovy-all-jdk14 库添加到类路径。

你可以在获得groovy图书馆。

UPDATED:
For solving net.sf.jasperreports.engine.JRException: No report compiler set for language : null problem you can try to set groovy report language and add groovy-all-jdk14 library to classpath.
You can get groovy library here.

报告标题样本,语言设置为 groovy

The sample of report header with language set to groovy:

<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"
...
language="groovy"
...>

这篇关于JasperReports类路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 04:43