1

I am trying to read decrypted property file.

File location is /WEB-INF/classes/db.properties

Decoder java file is src/a2.util/Decoder.java

which has method called readFile(String fileName).

When I call the method like this:

Decoder.readFile("db.properties")

I got file not found exception. saying..

db.properties (System cannot find specified file)

Can someone help me with this ?

user207421
  • 305,947
  • 44
  • 307
  • 483
David
  • 13
  • 3
  • 1
    Resources are not files, and `/WEB-INF/classes` is not the current working directory when your servlet executes. – user207421 Nov 17 '16 at 07:15

1 Answers1

0

You can not access as above.

You can get real path as:

ServletContext context = this.getServlet().getServletContext();     
String fullPath = context.getRealPath("/WEB-INF/classes/db.properties");

OR

java.net.URL url = [ClassName].class.getClassLoader().getResource("/WEB-INF/classes/db.properties"); 

Unless try with this as:

InputStream inputStream = servletContext.getResourceAsStream("/WEB-INF/classes/db.properties");

OR

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("/db.properties");
Chandana Kumara
  • 2,485
  • 1
  • 22
  • 25