Not able to find resource file in classpath

Answered

Even though property files are in the classpath, tomcat is not able to load that files at runtime, and I am getting below error while tomcat startup.

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
14:46:05.603 [RMI TCP Connection(2)-127.0.0.1] ERROR org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: /Users/300004204/Library/Caches/IntelliJIdea2018.3/tomcat/Unnamed_pricingengine-parent_2/conf/serviceurls.properties (No such file or directory)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:148) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:283) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:163) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:687) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:525) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5157) ~[catalina.jar:7.0.92]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5680) ~[catalina.jar:7.0.92]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145) ~[catalina.jar:7.0.92]
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:1018) ~[catalina.jar:7.0.92]
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:994) ~[catalina.jar:7.0.92]
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652) ~[catalina.jar:7.0.92]
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1900) ~[catalina.jar:7.0.92]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_191]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_191]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_191]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_191]
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301) ~[tomcat-coyote.jar:7.0.92]
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) ~[?:1.8.0_191]
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) ~[?:1.8.0_191]
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:619) ~[catalina.jar:7.0.92]
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:566) ~[catalina.jar:7.0.92]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_191]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_191]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_191]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_191]
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301) ~[tomcat-coyote.jar:7.0.92]
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) ~[?:1.8.0_191]
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) ~[?:1.8.0_191]
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468) ~[?:1.8.0_191]
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76) ~[?:1.8.0_191]
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309) ~[?:1.8.0_191]
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401) ~[?:1.8.0_191]
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829) ~[?:1.8.0_191]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_191]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_191]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_191]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_191]
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357) ~[?:1.8.0_191]
at sun.rmi.transport.Transport$1.run(Transport.java:200) ~[?:1.8.0_191]
at sun.rmi.transport.Transport$1.run(Transport.java:197) ~[?:1.8.0_191]
at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_191]
at sun.rmi.transport.Transport.serviceCall(Transport.java:196) ~[?:1.8.0_191]
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573) ~[?:1.8.0_191]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:834) ~[?:1.8.0_191]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688) ~[?:1.8.0_191]
at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_191]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687) [?:1.8.0_191]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_191]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_191]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_191]
Caused by: java.io.FileNotFoundException: /Users/300004204/Library/Caches/IntelliJIdea2018.3/tomcat/Unnamed_pricingengine-parent_2/conf/serviceurls.properties (No such file or directory)
at java.io.FileInputStream.open0(Native Method) ~[?:1.8.0_191]
at java.io.FileInputStream.open(FileInputStream.java:195) ~[?:1.8.0_191]
at java.io.FileInputStream.<init>(FileInputStream.java:138) ~[?:1.8.0_191]
at java.io.FileInputStream.<init>(FileInputStream.java:93) ~[?:1.8.0_191]
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90) ~[?:1.8.0_191]
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188) ~[?:1.8.0_191]
at org.springframework.core.io.UrlResource.getInputStream(UrlResource.java:169) ~[spring-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:154) ~[spring-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98) ~[spring-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:177) ~[spring-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:158) ~[spring-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:139) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
... 52 more


Tomcat Version # 7.0.92
Java Version # 1.8
Intellij Idea version # 20198.3
OS # MAC

Resource Configuration

 

Compiled resource in target directory

 

2
5 comments

How do you reference the file in source code? 

To add file to the classpath you need to make sure it is located in folder under the project content root (as sources or resources type as you have it on the screenshot) http://www.jetbrains.com/idea/webhelp/configuring-content-roots.html . IDEA then, when compiling the project and building the artifact will copy this file to the classpath.

For example if you use maven place the file in src/main/resources and reference it as /WEB-INF/classes/springcontext-servlet.xml or in wabapp/WEB-INF/springcontext-servlet.xml and reference as /WEB-INF/springcontext-servlet.xml in your String configuration.

Check also this article about working with Tomcat classpath http://www.mulesoft.com/tomcat-classpath . For information on how the classpath is being used in Spring application configuration descriptors, check some SO questions for example http://stackoverflow.com/questions/9092713/using-classpath-in-spring .

0
Avatar
Permanently deleted user

 

In context-web.xml, I am putting in classpath as mentioned below.

 

<bean id="propertyConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/serviceurls.properties</value>
...
0

This looks like incorrect referencing in config file, not related to IDE. Have you checked the https://stackoverflow.com/q/9092713/2000323 ? Check the actual location of the serviceurls.properties file in the produced artifact that gets deployed. Try /WEB-INF/classes/serviceurls.properties

0
Avatar
Permanently deleted user

@Andrey: What you are asking to try is contrary to what the stackoverflow link is pointing to right?

" The classpath root contains /WEB-INF/classes, so the path should be relative to that."  - From the link

From the screenshot above the file is already directly under classes directory and the path is also relative to that. 

0

Yes the classpath root is WEB-INF/classes for Tomcat. If you use maven/gradle place the file in src/main/filename and reference it as /WEB-INF/classes/springcontext-servlet.xml or in wabapp/WEB-INF/filename and reference as /WEB-INF/filename.

If you prepend the path with slash it is resolved from the root folder, so for

               <value>classpath:/serviceurls.properties</value>

try

               <value>classpath:serviceurls.properties</value>

 

0

Please sign in to leave a comment.