Question

I am using JasperReport for generation reports in Java Web Application.

I have a following line to get report file.

JasperReport report = (JasperReport) JRLoader.loadObject(getServletConfig().getServletContext().getRealPath("\rpts\report1.jasper"));

But when I try using above line I am getting FileNotFoundException. But when I try using following line, program executes successfully.

JasperReport report = (JasperReport) JRLoader.loadObject(getServletConfig().getServletContext().getRealPath("//rpts//report1.jasper"));

So, I want to know what is the problem with first line?

Thanks in advance...

Was it helpful?

Solution 2

The Path you give should be servlet path. Normally, servlets paths have forward slash /.

Please try this /rpts/report1.jasper".

OTHER TIPS

Java language (and others) needs \\ (double backslash) in Strings in order to read a single \. Your first way should change to

JasperReport report = (JasperReport) JRLoader.loadObject(getServletConfig()
    .getServletContext().getRealPath("\\rpts\\report1.jasper"))

Still, it would be better to check paths using / or even better using File#separator as shown here: File.separator vs Slash in Paths

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top