META-INF/persistence.xml cannot be opened because it does not exist

18,750

Your META-INF folder should be directly under the source folder, not to be the source folder by itself.

See Where do I put META-INF in Eclipse?

update From JPA 2.0 Specification

8.2 <...> A persistence unit is defined by a persistence.xml file. The jar file or directory whose META-INF directory contains the persistence.xml file is termed the root of the persistence unit. In Java EE environments, the root of a persistence unit must be one of the following:

  • an EJB-JAR file
  • the WEB-INF/classes directory of a WAR file
  • a jar file in the WEB-INF/lib directory of a WAR file
  • a jar file in the EAR library directory
  • an application client jar file

8.2.1 <...> A persistence.xml file defines a persistence unit. The persistence.xml file is located in the META-INF directory of the root of the persistence unit.

So it makes no difference how you configure your project, it's only the resulting jar that matters. If you're using maven, a common practice is to use src/main/resources folder for META-INF.

Share:
18,750
forhas
Author by

forhas

Updated on June 04, 2022

Comments

  • forhas
    forhas almost 2 years

    I'm creating a basic spring-maven project that should run as a java application (in process, not over a web server).

    My application context resides under the resources folder which is in my classpath:

    <?xml version="1.0" encoding="UTF-8"?>
    <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">
    
    
    <!-- Root Context: defines shared resources visible to all other web components -->
    
    <!-- Context -->
    <context:component-scan base-package="me.co.nutrition" />
    
    <!-- Properties -->
    <context:property-placeholder
        location="classpath:/nutrition.accumulation.properties" />
    
    <!-- DB -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    
    <bean id="persistenceUnitManager"
        class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
        <property name="defaultPersistenceUnitName" value="nutrition-pu"/>
        <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
        <property name="defaultDataSource" ref="dataSource" />
    </bean>
    
    <!-- JPA -->
    <bean id="entityManager"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitManager" ref="persistenceUnitManager" />
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
    </bean>
    
    <!-- jpaVendorAdapter (works in conjunction with the persistence.xml) -->
    <bean id="jpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="database" value="${jpa.database}" />
        <property name="showSql" value="${jpa.showSql}" />
        <property name="databasePlatform" value="${jpa.dialect}" />
        <property name="generateDdl" value="${jpa.generateDdl}" />
    </bean>
    
    <!-- Transactions -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManager" />
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <bean id="persistenceAnnotation" class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    
    </beans>
    

    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.

    Here is my persistence.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <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">
    
    <persistence-unit name="nutrition-pu" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
    </persistence-unit>
    </persistence>
    

    And as mentioned, it is under META-INF folder which is in my classpath. Here is my .classpath file:

    <?xml version="1.0" encoding="UTF-8"?>
    <classpath>
        <classpathentry kind="src" output="target/classes" path="src/main/java"/>
        <classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
        <classpathentry kind="src" path="META-INF"/>
        <classpathentry kind="src" path="resources"/>
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
        <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
        <classpathentry kind="output" path="target/classes"/>
    </classpath>
    

    you can see that the META-INF folder is there. I created a simple main that runs:

    public static void main(String[] args) throws Exception {
    
                ApplicationContext context = new ClassPathXmlApplicationContext("accumulationApplicationContext.xml");
    
    ...
    }
    

    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:

    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)
    

    I can't understand the problem. I'm quite sure i have the right structure, why can't my persistence.xml load?

    Thanks in advance!

    UPDATE:

    Thanks to Boris 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. This is the right structure. I hope this helps someone..

  • forhas
    forhas over 11 years
    Thakns. I created my META-INF by right clicking my src/main/java folder, creating a new source folder called META-INF. By doing this the new folder was created as a source folder that is in my classpath. As I understand the important thing is that it will be undeer my classpath, no matter under which hierarchy (isn't this the purpose of classpathXmlApplicationContext?). Anyways, what should I do de-facto to make it right?
  • Boris Treukhov
    Boris Treukhov over 11 years
    The purpose of Classpathxmlapplicationcontext is to load xml configuration from the classpath, and not from the file system like FileSystemXmlApplicationContext does. There's no need to put your Spring configuration files to META-INF, it will be picked up from any place in your classpath. P.S. Do not confuse spring bean configuration with the persistance.xml which location is regulated by JPA standards.
  • forhas
    forhas over 11 years
    Hi Boris, I did mixed it up a bit, thanks. Anyways, My application context is under the resources folder which I created in the same way I created the META-INF folder. The Classpathxmlapplicationcontext gest loaded just fine from the resources folder but the persistence.xml cannot be loaded from the META-INF folder, which is the issue.. I wonder if I should just find a more suitable maven Archetype for this purpose. I used the Quick Start Archetype which doesn't create a META-INF folder. Do yo know a better Archetype for my purpose? Could this be the issue?
  • forhas
    forhas over 11 years
    This is not a web-app, there is no application server involved here, so JEE related answers don't exactly fit (I have no war file..). I red the documentation, I need a clear answer which I still cant seem to find. What should I do in order to relocate my META-INF folder with Eclipse? As you can see, it is in my classpath at the moment, where else should I put it?
  • Boris Treukhov
    Boris Treukhov over 11 years
    @YogevLidor Pls rephrase - META-INF is a part of JAR file specification you can't relocate it inside jar file. Basically everything in the source files will be copied to the resulting jar, so the content of sourcefolder/META-INF/something* will land to jarfile.jar/META-INF/something* When java application is started, typically your jar file will be specified to be the class path java -classpath jarfile.jar MainClass and finally the resource will be picked by Spring with the class loader.
  • Boris Treukhov
    Boris Treukhov over 11 years
    @YogevLidor have you tried to open your jar file with an archiver tool? There you can find out where the persistence.xml was placed. If it's not there then you should configure your build tool to include the appropriate resources, for example stackoverflow.com/questions/3507048/….