0

Any other servlet is running fine on eclipse-apache/tomcat integration. But when I add a properties file in one of the classes, i get this error-

HTTP Status 500 - type Exception report

MessageDescription: The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.io.FileNotFoundException: xyz.properties(The system cannot find the file specified) java.io.FileInputStream.open(Native Method) java.io.FileInputStream.(Unknown Source) java.io.FileInputStream.(Unknown Source) xyz.DatabaseConnection(DatabaseAccess.java:23) xyz.HelloServlet.doGet(HelloServlet.java:22) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

Do I have to do some kind of modification in the web.xml file to let it know where the xyz.properties file exists ?

P.S - file and package name has been censored with "xyz".

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
kaustav datta
  • 687
  • 2
  • 11
  • 31

2 Answers2

0

You don not need to edit the web.xml.

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Properties properties = new Properties();
properties.load(classLoader.getResourceAsStream("xyz.properties"));

This assumes that the xyz.properties file is inside the classes folder.

Petter
  • 1,327
  • 11
  • 12
-1

I think the property file is not in the classpath. Please try putting the properties file WEIB-INF/classes folder and see if this resolves this issue.

Santosh
  • 17,667
  • 4
  • 54
  • 79
  • Can you please make sure that the properties file name referred in code and actual file name are same ? – Santosh Sep 21 '12 at 10:49
  • yup they're the same. I have used the properties files in another class, which works fine. Just the servlet class is not working. – kaustav datta Sep 21 '12 at 10:55
  • Just a debug step: Try giving the an absolute path of the properties file in Servlet and see if it loads it properly. – Santosh Sep 21 '12 at 11:01
  • @RaulGogo It was just a suggestion and not a rule and I understand that. File can be anywhere. This does not call for downvote for sure. – Santosh Sep 21 '12 at 11:53
  • What I understood was that it was a rule for the properties file to be in the WEB-INF folder, that's the reason for the downvote. Unfortunately, except that the post is edited, I cannot remove my downvote. – Raul Rene Sep 21 '12 at 11:58
  • No, I just said that I cannot remove my -1 unless the post is edited. Once you cast a vote and 5 minutes pass, your vote becomes locked. You can no longer change it unless the post is edited. – Raul Rene Sep 21 '12 at 12:20
  • @Santosh Can you tell me how to give an absolute path to the properties file in the servlet ? – kaustav datta Sep 23 '12 at 05:31
  • @kaustavdatta [Here is link](http://tutorials.jenkov.com/java-io/file.html) to an example of loading file using absolute path. – Santosh Sep 23 '12 at 11:27