Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with using getResourceAsStream

 
Veera Sundar
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.
 
Amit Ghorpade
Bartender
Posts: 2854
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,
 
Ben Souther
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: 2854
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
 
David O'Meara
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.
 
Adeel Ansari
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.
 
abhishek paul
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