title: Tomcat + JPA + MySQL : java.lang.NoClassDefFoundError: javax/persistence/Persistence
body: <p>as part of a school project, I must connect a java REST API to a MySql database.</p>
  
  <p>Unfortunately, I can't seem to instantiate the EntityManagerFactory.
  I've already tried to find the solution online, but I couldn't fix my issues.</p>
  
  <p>I have several classes that will need to be stored in the datacase, and I've used annotations to describe them (basically @Entity annotations, @ManyToOne and others, and of course @Id and @GeneratedValue</p>
  
  <p>My custom EntityManager :</p>
  
  <pre><code>import javax.persistence.EntityManager;
  import javax.persistence.EntityManagerFactory;
  import javax.persistence.Persistence;
  
  public class NatagoraEntityManager {
      private EntityManager manager;
      public NatagoraEntityManager(){
          EntityManagerFactory factory =  Persistence.createEntityManagerFactory("MyPersistenceUnit");
          manager = factory.createEntityManager();
      }
  }
  </code></pre>
  
  <p>My RestController :</p>
  
  <pre><code>@Path("/helloworld")
  public class HelloWorld {
  
      private NatagoraEntityManager manager;
      public HelloWorld(){
          //Without this line it works fine
          manager = new NatagoraEntityManager();
      }
  
  
      // The Java method will process HTTP GET requests
      @GET
      // The Java method will produce content identified by the MIME Media type "text/plain"
      @Produces("text/plain")
      public String getClichedMessage() {
          // Return some cliched textual content
          return "Hello World";
      }
  }
  </code></pre>
  
  <p>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" version="2.0"&gt;
  
      &lt;persistence-unit name="MyPersistenceUnit"&gt;
          &lt;properties&gt;
  
              &lt;property name="openjpa.ConnectionURL" value="jdbc:mysql://111.111.111.111:3306/bdname/"/&gt;
              &lt;property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/&gt;
              &lt;property name="openjpa.ConnectionUserName" value="login"/&gt;
              &lt;property name="openjpa.ConnectionPassword" value="password"/&gt;
              &lt;property name="openjpa.Log" value="DefaultLevel=TRACE, Tool=INFO"/&gt;
              &lt;property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>My pom.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;JAXRS-Example&lt;/groupId&gt;
      &lt;artifactId&gt;JAXRS-Example&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.sun.jersey&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-server&lt;/artifactId&gt;
              &lt;version&gt;1.19&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.sun.jersey&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-servlet&lt;/artifactId&gt;
              &lt;version&gt;1.19&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.owlike&lt;/groupId&gt;
              &lt;artifactId&gt;genson&lt;/artifactId&gt;
              &lt;version&gt;0.99&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.sun.jersey&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-client&lt;/artifactId&gt;
              &lt;version&gt;1.19&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.sun.jersey&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-json&lt;/artifactId&gt;
              &lt;version&gt;1.9&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.persistence&lt;/groupId&gt;
              &lt;artifactId&gt;persistence-api&lt;/artifactId&gt;
              &lt;version&gt;[1.0.2,)&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.openjpa&lt;/groupId&gt;
              &lt;artifactId&gt;openjpa-persistence-jdbc&lt;/artifactId&gt;
              &lt;version&gt;[1.0.1,)&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;sourceDirectory&gt;src&lt;/sourceDirectory&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.6&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;warSourceDirectory&gt;WebContent&lt;/warSourceDirectory&gt;
                      &lt;failOnMissingWebXml&gt;false&lt;/failOnMissingWebXml&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.3&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;source&gt;1.7&lt;/source&gt;
                      &lt;target&gt;1.7&lt;/target&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>And finally, here is the errors I receive :</p>
  
  <pre><code> java.lang.NoClassDefFoundError: javax/persistence/Persistence
      at DAL.NatagoraEntityManager.&lt;init&gt;(NatagoraEntityManager.java:10)
      at main.java.HelloWorld.&lt;init&gt;(HelloWorld.java:20)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
      at com.sun.jersey.server.spi.component.ResourceComponentConstructor._construct(ResourceComponentConstructor.java:245)
      at com.sun.jersey.server.spi.component.ResourceComponentConstructor.construct(ResourceComponentConstructor.java:233)
      at com.sun.jersey.server.impl.resource.PerRequestFactory$PerRequest._getInstance(PerRequestFactory.java:182)
      at com.sun.jersey.server.impl.resource.PerRequestFactory$AbstractPerRequest.getInstance(PerRequestFactory.java:144)
      at com.sun.jersey.server.impl.application.WebApplicationContext.getResource(WebApplicationContext.java:239)
      at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:83)
      at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
      at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
      at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
      at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
      at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
      at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
      at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
      at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
      at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
      at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.lang.Thread.run(Thread.java:748)
  Caused by: java.lang.ClassNotFoundException: javax.persistence.Persistence
      at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1339)
      at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
      ... 43 more
  </code></pre>
  
  <p>If you find an answer somewhere, I'll be grateful, as I'm stuck since several hours on this problem.
  If you need more informations, I'll be happy to tell you what you need.</p>
  
  <p>Thank you !</p>
  
  <p>EDIT : 
  Here is a part of my project.iml file where I choose the JPA provider :</p>
  
  <pre><code>&lt;facet type="jpa" name="JPA"&gt;
        &lt;configuration&gt;
          &lt;setting name="validation-enabled" value="true" /&gt;
          &lt;setting name="provider-name" value="OpenJPA" /&gt;
          &lt;datasource-mapping /&gt;
          &lt;deploymentDescriptor name="persistence.xml" url="file://$MODULE_DIR$/src/META-INF/persistence.xml" /&gt;
        &lt;/configuration&gt;
      &lt;/facet&gt;
  </code></pre>
  
  <p>EDIT2 : changed the pom.xml</p>
  
  <p>EDIT3 : Here are the jars included in my WAR file :
  <a href="https://i.stack.imgur.com/QheYQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QheYQ.png" alt="First part of the libraries"></a>
  <a href="https://i.stack.imgur.com/Xt1nb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Xt1nb.png" alt="Second part of the libraries"></a></p>