aspose file tools
The moose likes Servlets and the fly likes Reading properties file inside /WEB-INF Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Reading properties file inside /WEB-INF" Watch "Reading properties file inside /WEB-INF" New topic
Author

Reading properties file inside /WEB-INF

Tien Shan
Greenhorn

Joined: Oct 08, 2004
Posts: 19
I am trying to read a properties file ("my.properties"), which I put right under /WEB-INF when I created an eclipse project.

I told Spring where the file is like this:
<bean id="propertyPlaceholderConfigurer" class="xx..
<property name="locations">
<list>
<value>/WEB-INF/my.properties</value>
</list>
</property>
</bean>

I created a war file, and deployed under /webapps.

Inside some class, which is an ordinary java class (not a servlet), I wanted to read that "my.properties" file like this:



When I "System.out" inputStream, it is null.

another log file (cas.log) has this error:


I spent a day googling, but could not make it work-- that's why I am here.

What's happening? Can someone give me some pointers?

Thanks!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56157
    
  13

The classloader will look in the classpath, not the web root.

If you want to load a resource from the web root, use the servlet context, not the class loader.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Tien Shan
Greenhorn

Joined: Oct 08, 2004
Posts: 19
Thanks Bear Bibeault.
I moved the my.properties file inside /WEB-INF/classes, and that did help to get rid of the cas.log error.

Now, how do I get rid of the catalina.out error? It still says inputstream is null.

PS: My spring config now looks like:
<value>/WEB-INF/classes/my.properties< /value>

PPS: My java file is simple java file, does not extend or implement any ServletXX stuff.
Tien Shan
Greenhorn

Joined: Oct 08, 2004
Posts: 19
Latest construction:
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("classes/my.properties");

is giving me null.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16482
    
    2

Your my.properties file is in the WEB-INF/classes folder? Then:

Tien Shan
Greenhorn

Joined: Oct 08, 2004
Posts: 19
Paul, it works.
Cannot thank you enough!

Cheers.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 14456
    
    7

Actually, an even better way is to use the ServletContext getResourceAsStream method. The path you supply to that method is "/WEB-INF/my.properties".

Using the classloader mechanism not only is more general-java and less J(2)EE, but it also doesn't work for non-classpath resources.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading properties file inside /WEB-INF
 
Similar Threads
Resource bundle properties file in Spring MVC
Unit tetsing (Junit) Spring Framework
Load a properties file in application context
Reading file from tomcat, outside servelet.
Spring3/JPA2 configuration problem