Search code examples
jasper-reportsicefaces

How to read a file from WEB-INF/resources folder?


I am using Icefaces for webapplication development. I wish to read a file from the resources folder and use it in the sessionbean.

Actually I wish to setup Jasper Reports. I have already setup the libraries in the classpath. The problem I get is while fetching the file from /WEB-INF/resources/ folder. Everytime I run the code from SessionBean, I get the exception:

net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException: /resources/reports/myreport.jrxml (No such file or directory)

The Code I use is:

public void generateReport() {
    try {
            JasperCompileManager.compileReportToFile(
                    "/resources/reports/myreport.jrxml",
                    "/resources/reports/myreport.jasper");
        } catch (Exception e) {
            e.printStackTrace();
        }
}

The above code is in the SessionBean. Plz help


Solution

  • You are passing relative URLs to method JasperCompileManager.compileReportToFile. This method expects filenames as parameters, not URLs.

    The solution suggested in other internet forums is:

    JasperCompileManager.compileReportToFile(
      getServletContext().getRealPath(xmlFile), 
      getServletContext().getRealPath(compiledFile));