This week's book giveaway is in the HTML, CSS and JavaScript forum.
We're giving away four copies of hapi.js in Action and have Matt Harrison on-line!
See this thread for details.
Win a copy of hapi.js in Action this week in the HTML, CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading properties file inside /WEB-INF

 
Tien Shan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65417
100
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tien Shan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Latest construction:
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("classes/my.properties");

is giving me null.
 
Paul Clapham
Sheriff
Posts: 21695
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your my.properties file is in the WEB-INF/classes folder? Then:

 
Tien Shan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul, it works.
Cannot thank you enough!

Cheers.
 
Tim Holloway
Saloon Keeper
Posts: 18386
57
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!