JasperReports 类路径

发布于 2024-12-10 19:02:31 字数 1057 浏览 0 评论 0原文

我想将 .jrxml 文件包含在我的 NetBeans swing 项目中。我使用 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<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 文件?

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();
    }

Then the following error is given;

net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException: rep\rp.jrxml (The system cannot find the path specified)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月野兔 2024-12-17 19:02:31

此代码适用于我:

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: No report compiler set for language : null 问题,您可以尝试设置 groovy 报告语言并添加 groovy- all-jdk14 库到类路径。
您可以在此处获取groovy库。

语言设置为 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" 
...>

This code works for me:

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());
        }
    }
}

My project structure:

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

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.

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" 
...>
腻橙味 2024-12-17 19:02:31

这里是解决方案。最好在 jar 文件本身中提供绝对路径。另外,当运行 jar 时,最好使用 *.jasper 文件而不是 .jrxml,它也可能会导致生成报告的速度然后 .jasper 可以作为输入流发送到 JasperFillManager。就是这样。

Here the solution.It is better to provide an absolute path in the jar file itself.Also when the jar is run,using *.jasper file is better to use instead of .jrxml it may cause for the speed of generating the report as well.Then that .jasper can be sent to JasperFillManager as a inputstream. That is it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文