Could not open ServletContext resource
Asked Answered
K

8

41

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

Kajdan answered 17/5, 2011 at 18:31 Comment(11)
Are you sure that classpath: prefix is actually there?Roby
yes, after build it tells me /home/...-DEVELOPMENT-0.0.1.war build successful. When I open the war and look at /WEB-INF/classes/application-context.xml i see the classpath: prefixKajdan
you say ".xml and .properties files are both in /WEB-INF/classes" - does that mean that "social.properties" specifically is in /WEB-INF/classes?Particiaparticipant
yes social.properties (and also log4j.properties - but it does not matter) and also application-context.xml are in /WEB-INF/classesKajdan
Doesn't application-context.xml go in /WEB-INF and not /WEB-INF/classes?Slander
@Slander no it should go in classesDialogist
it can go wherever you tell it to. By default it's in /WEB-INFChristan
I just uploaded the minimized (deleted libs) war package on my university webpage [link]fi.muni.cz/~xkunc7/develop.war if it could help to anyone (i mean me :-) )Kajdan
Maybe you need a forward slash to say that the properties file is located at the root of the classpath? classpath:/social.propertiesSlander
Pom is in the develop.war also. Look at META-INF.Kajdan
What is the log output when you set the log level to debug?Alloy
A
36

Do not use classpath. This may cause problems with different ClassLoaders (container vs. application). WEB-INF is always the better choice.

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-config.xml</param-value>
</context-param>

and

<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
     <property name="location">
         <value>/WEB-INF/social.properties</value>
     </property>
</bean>
Agoraphobia answered 7/2, 2012 at 16:3 Comment(3)
How can we use BufferedWriter for a file loaded in this manner?Hegira
@user75782131 you could try getServletContext().getRealPath("/WEB-INF/somefile.txt")Agoraphobia
for the above since the file are inside classes should be use <param-value>/WEB-INF/classes/spring-config.xml</param-value>Expurgate
L
18

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

Leatri answered 9/11, 2011 at 11:45 Comment(1)
I've a json file path in properties.Journalistic
B
8

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.

Broadloom answered 25/11, 2011 at 17:42 Comment(0)
S
1

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!

Shanta answered 21/3, 2019 at 1:52 Comment(0)
C
0

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.

Conveyancer answered 18/5, 2011 at 21:58 Comment(0)
P
0

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.

Palestra answered 27/5, 2011 at 16:30 Comment(0)
M
0

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>
Momentous answered 20/10, 2011 at 9:35 Comment(0)
K
0

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>
Kame answered 25/9, 2013 at 5:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.