title: Why did I get this exception when running JPA?
body: <p>This is just a normal JPA console built by Maven. I got a very annoying exception </p>
  
  <pre><code>java.lang.ClassNotFoundException: javax.persistence.Persistence
  </code></pre>
  
  <p>I found some solutions about that such problem and they suggest adding <strong><em>javaee-api</em></strong> lib to pom.xml. But the error still stands with <strong><em>javee-api lib</em></strong>.</p>
  
  <pre><code>import javax.persistence.EntityManager;
  import javax.persistence.EntityTransaction;
  import javax.persistence.EntityManagerFactory;
  import javax.persistence.Persistence;
  
  public class App 
  {
      public static void main( String[] args )
      {
          // This line causes error
          EntityManagerFactory entityManagerFactory =
                      Persistence.createEntityManagerFactory("testjpa");
          // It works when I comment above line and print hello
      }
  }
  </code></pre>
  
  <p>So I'm pretty sure that the exception doesn't relate to library issues and the persistence.xml file might cause the problem.</p>
  
  <p>Here my project tree</p>
  
  <pre><code>├── pom.xml
  └── src
      ├── main
      │   ├── java
      │   │   └── com
      │   │       └── hoan
      │   │           ├── App.java
      │   │           └── Customer.java
      │   └── resources
      │       └── META-INF
      │           └── persistence.xml
      └── test
  </code></pre>
  
  <p>My persistence.xml</p>
  
  <pre><code>&lt;persistence-unit name="testjpa" transaction-type="RESOURCE_LOCAL"&gt;
  
      &lt;provider&gt;org.apache.openjpa.persistence.PersistenceProviderImpl&lt;/provider&gt;
  
      &lt;class&gt;com.foo.Customer&lt;/class&gt;
  
      &lt;properties&gt;
          &lt;property name="javax.persistence.jdbc.driver" 
                    value="com.mysql.jdbc.Driver"/&gt;
          &lt;property name="javax.persistence.jdbc.url"
                    value="jdbc:mysql://localhost:3306/quickstart"&gt;
          &lt;property name="javax.persistence.jdbc.user" value="root" /&gt;
          &lt;property name="javax.persistence.jdbc.password" value="password"/&gt;
      &lt;/properties&gt;
  
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p>pom.xml dependencies</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;org.apache.openjpa&lt;/groupId&gt;
      &lt;artifactId&gt;openjpa-all&lt;/artifactId&gt;
      &lt;version&gt;2.0.0&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
      &lt;groupId&gt;mysql&lt;/groupId&gt;
      &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
      &lt;version&gt;5.1.22&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>By the way I followed this thread <a href="https://stackoverflow.com/questions/4261435/no-persistence-provider-for-entitymanager-error">&quot;No Persistence provider for EntityManager&quot; error</a> which has exactly same my problem, but nothing seems to work.</p>
  
  <p>What's the problem?</p>
  
  <p>Updated: the command line to run jar file</p>
  
  <pre><code>java -cp target/jpa-demo.jar com.foo.App
  </code></pre>