File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Problem with using getResourceAsStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem with using getResourceAsStream" Watch "Problem with using getResourceAsStream" New topic
Author

Problem with using getResourceAsStream

Veera Sundar
Greenhorn

Joined: Jun 08, 2007
Posts: 25
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.


Thanks,<br />Veera |<a href="http://veerasundar.com/blog" target="_blank" rel="nofollow">Blog</a>
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

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




Hope this helps


SCJP, SCWCD.
|Asking Good Questions|
Veera Sundar
Greenhorn

Joined: Jun 08, 2007
Posts: 25
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

Joined: Dec 11, 2004
Posts: 13410

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?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Aug 15, 2004
Posts: 2874
Hence, a good place could be, /WEB-INF/classes/resources/.../xyz.properties

Cheers.
abhishek paul
Greenhorn

Joined: Sep 26, 2006
Posts: 25
..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);

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


SCJP5 -
To repeat what others have said, requires education, to challenge it , requires brains.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with using getResourceAsStream