Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Problem with using getResourceAsStream  RSS feed

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
the path you are providing to the function is wrong, correct it.




Hope this helps
 
Veera Sundar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hence, a good place could be, /WEB-INF/classes/resources/.../xyz.properties

Cheers.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
..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);

-----------------------------------------------------------------------------
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!