I am trying to retrieve a jrxml file in a relative path using the following java code:
String jasperFileName = \"/web/WEB-INF/reports/MemberOrderListReport.jrxm
You're trying to treat the jrxml
file as an object on the file-system, but that's not applicable inside a web application.
You don't know how or where your application will be deployed, so you can't point a File
at it.
Instead you want to use getResourceAsStream
from the ServletContext
. Something like:
String resourceName = "/WEB-INF/reports/MemberOrderListReport.jrxml"
InputStream is = getServletContext().getResourceAsStream(resourceName);
is what you're after.
You should place 'MemberOrderListReport.jrxml' in classpath, such as it being included in a jar placed in web-inf\lib or as a file in web-inf\classes. The you can read the file using the following code:
InputStream is=YourClass.class.getClassLoader().getResourceAsStream("MemberOrderListReport.jrxml");
I've seen this kind of problem many times, and the answer is always the same...
The problem is the file path isn't what you think it is. To figure it out, simply add this line after creating the File
:
System.out.println(report.getAbsolutePath());
Look at the output and you immediately see what the problem is.
check that your relative base path is that one you think is:
File f = new File("test.txt"); System.out.println(f.getAbsoluteFile());
String jasperFileName = "/web/WEB-INF/reports/MemberOrderListReport.jrxml";
Simple. You don't have a /web/WEB-INF/reports/MemoberOrderListReport.jrxml file on your computer.
You are clearly executing in a web-app environment and expecting the system to automatically resolve that in the context of the web-app container. It doesn't. That's what getRealPath()
and friends are for.