[java] Could not open ServletContext resource

This is quite similar question to one older but the solution did not work for me.

I have a WAR package.

In web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:application-context.xml</param-value>
</context-param>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

In application-context.xml

<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:social.properties</value>
    </property>
</bean>

But getting this:

org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/social.properties]

I checked the WAR package - .xml and .properties files are both in /WEB-INF/classes

.properties file is in src/main/resources and .xml in src/main/java (in default package both) and maven transports them (I think) correctly in the default package of WEB-INF/classes

Does anyone know why i could get this exception? Thank you.

EDIT: I just want to add that JUnit tests goes correctly (i mean they load what they should from social.properties) but when running the app it ignores my classpath: prefix

This question is related to java spring glassfish web.xml

The answer is


Mark sure propertie file is in "/WEB-INF/classes" try to use

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>/WEB-INF/classes/social.properties</value>
    </property>
</bean>

I think currently the application-context.xml file is into src/main/resources AND the social.properties file is into src/main/java... so when you package (mvn package) or when you run tomcat (mvn tomcat:run) your social.properties disappeared (I know you said when you checked into the .war the files are here... but your exception says the opposite).

The solution is simply to put all your configuration files (application-context.xml and social.properties) into src/main/resources to follow the maven standard structure.


try with this code...

 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
         <property name="ignoreUnresolvablePlaceholders" value="true"/>
             <property name="locations">
              <list>                        
                    <value>/social.properties</value>               
              </list>
         </property>         
        </bean>

I had the same error.

My filename was jpaContext.xml and it was placed in src/main/resources. I specified param value="classpath:/jpaContext.xml".

Finally I renamed the file to applicationContext.xml and moved it to the WEB-INF directory and changed param value to /WEB-INF/applicationContext.xml, then it worked!


Put the things like /src/main/resources/foo/bar.properties and then reference them as classpath:/foo/bar.properties.


Try to use classpath*: prefix instead.

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/resources.html#resources-classpath-wildcards

Also please try to deploy exploded war, to ensure that all files are there.


Are you having Tomcat unpack the WAR file? It seems that the files cannot be found on the classpath when a WAR file is loaded and it is not being unpacked.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to spring

Are all Spring Framework Java Configuration injection examples buggy? Two Page Login with Spring Security 3.2.x Access blocked by CORS policy: Response to preflight request doesn't pass access control check Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified Spring Data JPA findOne() change to Optional how to use this? After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName The type WebMvcConfigurerAdapter is deprecated No converter found capable of converting from type to type

Examples related to glassfish

Cannot start GlassFish 4.1 from within Netbeans 8.0.1 Service area How to configure Glassfish Server in Eclipse manually org.glassfish.jersey.servlet.ServletContainer ClassNotFoundException Location of GlassFish Server Logs How do I specify the JDK for a GlassFish domain? How to return a PNG image from Jersey REST service method to the browser Could not open ServletContext resource What is the difference between Tomcat, JBoss and Glassfish? Address already in use: JVM_Bind What does 'URI has an authority component' mean?

Examples related to web.xml

SEVERE: ContainerBase.addChild: start:org.apache.catalina.LifecycleException: Failed to start error Session TimeOut in web.xml How to configure welcome file list in web.xml What is the significance of url-pattern in web.xml and how to configure servlet? No WebApplicationContext found: no ContextLoaderListener registered? How to specify the default error page in web.xml? How to define servlet filter order of execution using annotations in WAR Loading context in Spring using web.xml Could not open ServletContext resource Why do we use web.xml?