java JasperReports 类路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7823756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:34:46  来源:igfitidea点击:

JasperReports class path

javaswingjasper-reportsireport

提问by Débora

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

我想在我的NetBeans摆动项目中包含.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<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 jrxmlfiles inside my NetBeansproject and use jrxmlfiles inside the project?

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

回答by Alex K

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 : nullproblem you can try to set groovyreport language and add groovy-all-jdk14library to classpath.
You can get groovy library here.

更新:
为了解决net.sf.jasperreports.engine.JRException: No report compiler set for language : null问题,您可以尝试设置groovy报告语言并将groovy-all-jdk14库添加到类路径。
你可以在这里获得 groovy 库。

The sample of report header with language set to 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" 
...>

回答by Débora

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.

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