在Java Web应用程序上使用JasperReports获取“文件未找到异常” [英] Getting 'File Not Found Exception' with JasperReports on Java Web Application

查看:195
本文介绍了在Java Web应用程序上使用JasperReports获取“文件未找到异常”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过预先设计的Jrxml文件从My Java Web Application创建Jasper报告。该文件位于名为jrxml的目录中的My web文件夹(Netbeans)中,因此我尝试使用此方法获取该文件。

I am creating Jasper reports from My Java Web Application through a pre-designed Jrxml file. the file is in My web folder (Netbeans) in a directory named jrxml so I am trying to get at it using this Method.

public void generateChurchReport(IncomeExpenseBean ieb) {
        church = ieb.getChurch();
        user = ieb.getUser();
        String currdate = dt.getCurrentDate();
        Connection conn = db.getDbConnection();
        Map parameters = new HashMap();
        try{ 
        parameters.put("ChurchName", church);
        JasperReport jasperReport = JasperCompileManager.compileReport("/jrxml/ChurchIncome_expenseReport.jrxml");
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn);
        File f = new File(user + church+ currdate +  ".pdf");
        JasperExportManager.exportReportToPdfFile(jasperPrint, f.getAbsolutePath());
        Desktop.getDesktop().open(new File(f.getAbsolutePath())); 
        }catch(Exception asd){
            System.out.println(asd.getMessage());
        }

    }

我收到文件未找到异常因为应用程序期望文件位于某处;

I am getting File Not Found Exception because the Application is expecting the file somewhere in ;

  C:\Program Files\glassfish-3.1.2.2\glassfish\domains\TestDom\jrxml\

如何在我的网络文件夹中阅读此文件如何在同一文件夹中创建报告?

How do i read this file in my web folder and How can I create the Reports Inside the same folder?

编辑如果我不提供任何路径我的报告将在C生成: \Program Files\glassfish-3.1.2.2\glassfish \domains\TestDom \如果jrxml文件位于该位置。

EDIT If I do not give Any Paths My reports are getting Generated at C:\Program Files\glassfish-3.1.2.2\glassfish\domains\TestDom\ if the jrxml file is in that Location.

推荐答案

你在这里做错了,你试图在你的java类的web文件夹中找到你的 jrxml 文件。由于上下文路径不正确,这肯定会在运行时引发文件未找到错误。您可以简单地执行以下操作: -

What you are doing wrong here is, you are trying to locate your jrxml file somewhere in web folder from your java class. This will definitely raise "File Not found error" at run time because of incorrect context path. You could simply do the following:-


  1. 在您的java类包下创建一个名为Jrxml的文件夹。假设java类包是 com.ejb.beans ,请创建一个文件夹 com.ejb.beans.jrxml

  2. 将所有 jrxml 文件放入此文件夹。

  3. 在您的java类中,加载类加载器并按名称找到 jrxml ,您将轻松访问它。以下是代码: -

  1. Make a folder named say "Jrxml" under your java classes package. Suppose java classes package is com.ejb.beans, make a folder com.ejb.beans.jrxml.
  2. Put all your jrxml files into this folder.
  3. In your java class, load the class loader and locate the jrxml by its name and you will easily access it. Here is the code:-

ClassLoader classLoader = getClass()。getClassLoader();

InputStream url = null;

url = classLoader.getResourceAsStream(Report.jrxml);

此url可用于将报告编译为: -

This url can be used to compile report as :-

JasperReport jasperReport = JasperCompileManager.compileReport(url);

要创建报告输出文件,可以将其存储在应用程序服务器的某个路径中。在环境变量中设置服务器路径并在运行时在类中提取它,如下所示: -

To create the report output file, you could store it at some path in your application server. Set your server path in environment variable and extract it in your class at runtime like this :-

String serverHomeDir = System.getProperty(server。 home.dir);

String reportDestination = serverHomeDir +/ domain / ReportOutput / renport.html;

// now print report at reportDestination
JasperExportManager.exportReportToHtmlFile(jasperPrint, reportDestination);

您的html文件将在所需的目的地生成,您可以轻松阅读并呈现它,你希望通过你的网页。

Your html file will be generated at the required destination, which you can easily read and render it, the way you want to, through your web page.

这篇关于在Java Web应用程序上使用JasperReports获取“文件未找到异常”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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