0

I am writing a java servlet that should generate a jasper report. I have stored the jrxml under the WebContent folder in a folder named jrxml (WebContent/jrxml/theFile.jrxml). First I used the following code to generate the report:

InputStream input = new FileInputStream(new File("/jrxml/employeesList.jrxml"));
JasperDesign jasperDesign = JRXmlLoader.load(input);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

However, I kept getting a "No such file or directory" error message. So I then used the following code which I took from Accessing properties file in a JSF application programmatically :

String path = "/jrxml/employeesList.jrxml";
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream input = loader.getResourceAsStream(path);
JasperDesign jasperDesign = JRXmlLoader.load(input);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

Now I no longer get a "No such file or directory" error message, but instead I get "net.sf.jasperreports.engine.JRException: java.net.MalformedURLException" exception, and according to the stack trace it is being cause by the fourth command (JRXmlLoader.load(input)). Can someone please tell me what I'm doing wrong?

Thanks

Community
  • 1
  • 1
user3245747
  • 805
  • 2
  • 17
  • 30

2 Answers2

1

If you want to load a file in your WebContent folder you have to use ServletContext.html#getRealPath to get the actual path of the file in the filesystem, so you can try the following.

String path = getServletContext().getRealPath("/jrxml/employeesList.jrxml");
InputStream input = new FileInputStream(new File(path));
JasperDesign jasperDesign = JRXmlLoader.load(input);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

OTOH

If ClassLoader is used your resource should be in classpath (i.e. WEB_INF\classes or in any of the jars in WEB-INF\lib)

seenukarthi
  • 8,241
  • 10
  • 47
  • 68
-2

Can you please try following..?

Specify full path such as..

InputStream input = new FileInputStream(new File("F:/jrxml/employeesList.jrxml"));
Raja Simon
  • 10,126
  • 5
  • 43
  • 74
amdg
  • 2,211
  • 1
  • 14
  • 25