Could not open ServletContext resource
Solution 1
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>
Solution 2
Put the things like /src/main/resources/foo/bar.properties
and then reference them as classpath:/foo/bar.properties
.
Solution 3
Try to use classpath*:
prefix instead.
Also please try to deploy exploded war, to ensure that all files are there.
kuncajs
Java, PHP and JavaScript developer mostly interested in complex Web applications, REST and Web systems.
Updated on March 21, 2020Comments
-
kuncajs about 4 years
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 insrc/main/resources
and.xml
insrc/main/java
(in default package both) and maven transports them (I think) correctly in the default package ofWEB-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 myclasspath:
prefix -
Admin over 10 yearsHow can we use
BufferedWriter
for a file loaded in this manner? -
Roman K over 10 years@user75782131 you could try
getServletContext().getRealPath("/WEB-INF/somefile.txt")
-
user2505915 almost 9 yearsfor the above since the file are inside classes should be use <param-value>/WEB-INF/classes/spring-config.xml</param-value>
-
Pavan over 3 yearsI've a json file path in properties.