Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
692 views
in Technique[技术] by (71.8m points)

jasper reports - Getting 'File Not Found Exception' with JasperReports on Java Web Application

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 Filesglassfish-3.1.2.2glassfishdomainsTestDomjrxml

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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. 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");

    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 + "/domains/ReportOutput/report.html";

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...