Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: class path resource cannot be resolved to URL because it dooes not exist

  1. #1
    Join Date
    Feb 2010
    Posts
    5

    Default class path resource cannot be resolved to URL because it dooes not exist

    Hello there, i am new to spring and i am having the following issue:

    I have an applicationContext.xml file in my WEB-INF folder, i also have a hibernate.cfg.xml file in my WEB-INF folder. I am using the following code in my applicationContext.xml file:
    Code:
    	<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        	<property name="jndiName" value="java:comp/env/jdbc/drinksDB" />
        	<property name="lookupOnStartup" value="false"/>
    		<property name="cache" value="true"/>
    		<property name="proxyInterface" value="javax.sql.DataSource"/>
    	</bean>
    
    
    	<bean id="sessionFactory"
    	class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="configLocation" value="classpath:Hibernate.cfg.xml" />
    </bean>
    	
    </beans>

    I have the following code in my Hibernate.cfg.xml file:

    Code:
    <hibernate-configuration>
    <session-factory>
    	<!-- Database properties -->
    	<property name="dialect">org.hibernate.dialect.OracleDialect</property> 
    	<property name="show_sql">true</property>
    	<property name="format_sql">true</property>
    	<property name="hibernate.hbm2ddl.auto">update</property>
    	
    	<!-- Mapping files -->
    	<mapping resource="hibernateRelationalMappings/Users.hbm.xml"/>
    	<mapping resource="hibernateRelationalMappings/Drinks.hbm.xml"/>
    </session-factory>
    </hibernate-configuration>
    However i get the following error:
    Code:
    SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [Hibernate.cfg.xml] cannot be resolved to URL because it does not exist
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1401)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:512)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:540)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:842)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:416)
    	at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:261)
    	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:192)
    	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3934)
    	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4429)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    	at org.apache.catalina.core.StandardHost.start(StandardHost.java:722)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    	at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    	at org.apache.catalina.core.StandardService.start(StandardService.java:516)
    	at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    	at org.apache.catalina.startup.Catalina.start(Catalina.java:583)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
    	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
    Caused by: java.io.FileNotFoundException: class path resource [Hibernate.cfg.xml] cannot be resolved to URL because it does not exist
    	at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:162)
    	at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:642)
    	at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1460)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1398)
    	... 27 more
    Any ideas on what i am doing wrong?

    Thanks in advance

  2. #2
    Join Date
    Jun 2009
    Posts
    190

    Default

    Hi,

    1) Can you ensure the file exists in classpath
    2) Try this http://jira.springframework.org/browse/SPR-895

    -Hetal

  3. #3
    Join Date
    Feb 2010
    Posts
    5

    Default

    Well, i am using Eclipse as my IDE, and applicationContext.xml seems to be getting picked up correctly, both applicationContext.xml and Hibernate.cfg.xml are in the WEB-INF folder, am i missing something?

  4. #4
    Join Date
    Jun 2009
    Posts
    190

    Default

    can y ou try classpath*:Hiernate.cfg.xml

  5. #5
    Join Date
    Feb 2010
    Posts
    5

    Default

    That also does not work, should the Hibernate.cfg.xml file be placed in the WEB-INF folder?

  6. #6
    Join Date
    Nov 2005
    Posts
    6

    Default

    we put hibernate.cfg.xml in the src folder. I think that spring listener is coded to look for applicationContext.xml in WEB-INF folder and that is why it finds it. I also think that WEB-INF directory is not added to the classpath for the web application.

    I would recommend moving the file to src folder.

  7. #7
    Join Date
    Feb 2010
    Posts
    5

    Default

    I have tried putting the Hibernate.cfg.xml file in the src folder and still no luck.

    I changed the code to the following:
    Code:
    	<bean id="sessionFactory"
    	class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="configLocation" value="oracleHibernate.cfg.xml" />
    	</bean>
    (removed the classpath: ) i even added classpath:* etc, still no luck

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    WEB-INF isn't part of the classpath. Only classes, lib and META-INF are on the classpath. If you want to load it from WEB-INF remove the classpath and simply put /WEB-INF/hibernate.cfg.xml .
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9
    Join Date
    Feb 2010
    Posts
    5

    Default

    Thanks Marten, however i have removed the classpath: and just moved the Hibernate.cfg.xml to the src folder, so

    value="Hibernate.cfg.xml"

    should work, but it does not, still getting the same error

  10. #10
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Sigh, reading seems to be hard...

    if you move hibernate.cfg.xml to the src folder and have ant/maven (depending on what you use) it is on the classpath so with your current setup it isn't going to be found because it expects it to be in the root of your WAR.

    As I stated if you want to load it from the root of the classpath, ie it is in /WEB-INF/classes use classpath: if you want to load it from /WEB-INF don't use classpath: and simply specify /WEB-INF/hibernate.cfg.xml.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •