[Logo]
Forums Register Login
Problem with using getResourceAsStream
Hi All,

I have a problem with my web application. My requirement is to read a properties file inside my WEB-INF directory. I am using the below code for this:



But, the above code is printing "Input Stream is null". I am clueless about why is it not reading my properties file.

Any help will be appreciated.

Thanks.
Hi,
the path you are providing to the function is wrong, correct it.




Hope this helps
Hi,

Thanks for the reply.


the path you are providing to the function is wrong, correct it.



Just to clarify:

the path should be "WEB-INF/SedApplication.properties". Is it correct?

Thanks,
 

Originally posted by Amit Ghorpade:
Hi,
the path you are providing to the function is wrong, correct it.



Amit,
How do you know that this path is wrong?
it was just a wild guess because i thought the function is unable to find the specified resource hence the NPE.

Am i wrong. If yes please correct me.



Thanks
The ClassLoader searches for the reseource on the web application classpath. By default this includes the WEB-INF/classes directory and the libraries in the WEB-INF/lib directory.

You may be confused with the ServletContext.getResource() and getResourceAsStream, they are slightly different.
Hence, a good place could be, /WEB-INF/classes/resources/.../xyz.properties

Cheers.
..as Adeel rightly says keep the properties file inside WEB-INF/classes/resources folder and the corresponding code would be :

------------------------------------------------------------------------------

ClassLoader cl = this.getClass().getClassLoader();
InputStream inputStream = cl.getResourceAsStream("/resources/SedApplication.properties");
System.out.println("Input Strem is : " + inputStream);

-----------------------------------------------------------------------------
Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss


This thread has been viewed 5892 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jul 17, 2018 09:17:25.