title: META-INF/persistence.xml cannot be opened because it does not exist
body: <p>I'm creating a basic spring-maven project that should run as a java application (in process, not over a web server).</p>
  
  <p>My application context resides under the resources folder which is in my classpath:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
      xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
      xmlns:tx="http://www.springframework.org/schema/tx" xmlns:ox="http://www.springframework.org/schema/oxm"
      xsi:schemaLocation="
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
     http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1xsd"&gt;
  
  
  &lt;!-- Root Context: defines shared resources visible to all other web components --&gt;
  
  &lt;!-- Context --&gt;
  &lt;context:component-scan base-package="me.co.nutrition" /&gt;
  
  &lt;!-- Properties --&gt;
  &lt;context:property-placeholder
      location="classpath:/nutrition.accumulation.properties" /&gt;
  
  &lt;!-- DB --&gt;
  &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"&gt;
      &lt;property name="driverClassName" value="${jdbc.driverClassName}" /&gt;
      &lt;property name="url" value="${jdbc.url}" /&gt;
      &lt;property name="username" value="${jdbc.username}" /&gt;
      &lt;property name="password" value="${jdbc.password}" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="persistenceUnitManager"
      class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager"&gt;
      &lt;property name="defaultPersistenceUnitName" value="nutrition-pu"/&gt;
      &lt;property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/&gt;
      &lt;property name="defaultDataSource" ref="dataSource" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- JPA --&gt;
  &lt;bean id="entityManager"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="persistenceUnitManager" ref="persistenceUnitManager" /&gt;
      &lt;property name="jpaVendorAdapter" ref="jpaVendorAdapter" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- jpaVendorAdapter (works in conjunction with the persistence.xml) --&gt;
  &lt;bean id="jpaVendorAdapter"
      class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
      &lt;property name="database" value="${jpa.database}" /&gt;
      &lt;property name="showSql" value="${jpa.showSql}" /&gt;
      &lt;property name="databasePlatform" value="${jpa.dialect}" /&gt;
      &lt;property name="generateDdl" value="${jpa.generateDdl}" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- Transactions --&gt;
  &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
  &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
      &lt;property name="entityManagerFactory" ref="entityManager" /&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="persistenceAnnotation" class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>I created a META-INF folder as a source folder and added this folder to my classpath. I created my persistence.xml file under this folder.</p>
  
  <p>Here is my persistence.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence xmlns="http://java.sun.com/xml/ns/persistence"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
      version="2.0"&gt;
  
  &lt;persistence-unit name="nutrition-pu" transaction-type="RESOURCE_LOCAL"&gt;
      &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
  &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>And as mentioned, it is under META-INF folder which is in my classpath. Here is my .classpath file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;classpath&gt;
      &lt;classpathentry kind="src" output="target/classes" path="src/main/java"/&gt;
      &lt;classpathentry kind="src" output="target/test-classes" path="src/test/java"/&gt;
      &lt;classpathentry kind="src" path="META-INF"/&gt;
      &lt;classpathentry kind="src" path="resources"/&gt;
      &lt;classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/&gt;
      &lt;classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/&gt;
      &lt;classpathentry kind="output" path="target/classes"/&gt;
  &lt;/classpath&gt;
  </code></pre>
  
  <p>you can see that the META-INF folder is there.
  I created a simple main that runs:</p>
  
  <pre><code>public static void main(String[] args) throws Exception {
  
              ApplicationContext context = new ClassPathXmlApplicationContext("accumulationApplicationContext.xml");
  
  ...
  }
  </code></pre>
  
  <p>As I run this, when trying to load the application context I get an exception that says the persistence.xml cannot be found or parsed:</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'persistenceUnitManager' defined in class path resource [accumulationApplicationContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Cannot parse persistence unit from class path resource [META-INF/persistence.xml]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
      ... 30 more
  Caused by: java.lang.IllegalArgumentException: Cannot parse persistence unit from class path resource [META-INF/persistence.xml]
      at org.springframework.orm.jpa.persistenceunit.PersistenceUnitReader.readPersistenceUnitInfos(PersistenceUnitReader.java:141)
      at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.readPersistenceUnitInfos(DefaultPersistenceUnitManager.java:380)
      at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.preparePersistenceUnitInfos(DefaultPersistenceUnitManager.java:341)
      at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.afterPropertiesSet(DefaultPersistenceUnitManager.java:326)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
      ... 37 more
  Caused by: java.io.FileNotFoundException: class path resource [META-INF/persistence.xml] cannot be opened because it does not exist
      at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158)
      at org.springframework.orm.jpa.persistenceunit.PersistenceUnitReader.readPersistenceUnitInfos(PersistenceUnitReader.java:129)
  </code></pre>
  
  <p>I can't understand the problem. I'm quite sure i have the <a href="http://i.cubeupload.com/rK1LQW.png" rel="nofollow noreferrer">right</a> structure, why can't my persistence.xml load?</p>
  
  <p>Thanks in advance!</p>
  
  <p>UPDATE:</p>
  
  <p>Thanks to <a href="https://stackoverflow.com/users/241986/boris-treukhov">Boris</a> I got it working. It a seems that I created the META-INF under the wrong path. This happened since I miss understood something with Eclipse project structure. <a href="http://i.cubeupload.com/546Cga.png" rel="nofollow noreferrer">This</a> is the right structure. I hope this helps someone..</p>