2

I'm getting the following error message when I try to start a spring web application:

2012-04-12 13:53:20,491 ERROR [org.springframework.web.servlet.DispatcherServlet] - 

Context initialization failed
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [timex-servlet.properties] cannot be opened because it does not exist
java.io.FileNotFoundException: class path resource [timex-servlet.properties] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:137)
    at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:172)

I'm running Tomcat (version 6.x) through eclipse. I tried putting timex-servlet.properties in the following directories but to no avail:

WebContent\WEB-INF
WebContent\WEB-INF\classes
WebContent\

Here is the reference to timex-servlet.properties in timex-servlet.xml:

    <bean id="placeholderConfig"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location"
        value="timex-servlet.properties" />
</bean>

There are several SO threads dealing with the same message that say to put a classpath: in front of the properties file reference. So I tried the following, which also did not work:

        <bean id="placeholderConfig"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location"
        value="classpath:timex-servlet.properties" />
</bean>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
opike
  • 7,053
  • 14
  • 68
  • 95
  • 2
    `WEB-INF\classes` is the correct location within the WAR file. `classpath:file.properties` is the correct way to indicate that the file will be located here. I can only suggest that Eclipse may be building your WAR differently than you expect. Does it work with stand-alone Tomcat? – Beau Grantham Apr 12 '12 at 21:51

6 Answers6

13

ensure the file is placed in the /WEB-INF/classes folder and use classpath:/timex-servlet.properties (note the slash after classpath:)

Chris White
  • 29,949
  • 4
  • 71
  • 93
3

If you do not want to move it under class and want to leave the properties file under WEB-INF/ try the following classpath:../file.properties

Sreesankar
  • 299
  • 2
  • 6
1

Should not place your properties file in the same location where your source code and main method is defined.It should be place in the same location where your Configuration file is located(applicationContext.xml).

In my case my properties file name is sport.properties (extension of the file should be .properties) and using maven for the project -

  1. Location of the properties file -> src->test->java->sport.properties

PropertiesFilelocation

0

For Spring projects, place the file under the src or src->somefolder->some.properties file.

Vijay
  • 1
0

In my case I was missing a cargo-maven2-plugin in my pom. Might help somebody who's stuck.

OAM
  • 105
  • 8
0

Just describing my case here, maybe it'll help someone :) Used Maven and Spring MVC with pure Java configuration (no XML). Here is my steps to resolve the issue:

1) Place your properties file in src/main/resources folder (create the last one manually if there is none).

Double check it, seriously! If you have the same level of attention as me, it can be a problem :)

2) Add to your Java configuration class the following annotation: @PropertySource("classpath:nameOfYourFile.properties")

3) Update your Maven project (in Eclipse: right-click on project -> Maven -> Update project).

And, of course, check that the name of your file matches the one in the annotation.
If anything else is OK with your project, then it should work.

LexSav
  • 444
  • 1
  • 9
  • 19