java.lang.ClassNotFoundException: javax.persistence.Entity

Problem

In JPA or Hibernate development, it hits the following error message :


Caused by: java.lang.ClassNotFoundException: javax.persistence.Entity
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	... 24 more

Solution

The javax.persistence.Entity is a class inside the J2EE SDK library “javaee.jar“, you are missing this jar file in your project classpath.

1. J2EE SDK

You can always get the javaee.jar from http://java.sun.com/javaee/. Download and install the SDK in your computer, the javaee.jar can be found in the “\J2EE_SDK_FOLDER\lib” folder. For example,


C:\Sun\SDK\lib\javaee.jar

Get the javaee.jar file and include it in your project classpath.

2. Java.Net Repository

Alternatively, you can get the “javaee.jar” from the java.net Maven


  <repositories>
  	<repository>
  		<id>Java.Net</id>
  		<url>http://download.java.net/maven/2/</url>
  	</repository>
  </repositories>
 
  <dependencies>
    <!-- Javaee API -->
	<dependency>
    	<groupId>javax</groupId>
    	<artifactId>javaee-api</artifactId>
    	<version>6.0</version>
	</dependency>
  </dependencies>
The downloaded java.net javaee.jar is not contains any method bodies, see this “how to get javaee.jar from Maven” article for detail.

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Dave Whaley
13 years ago

For Java EE 6, I found javax.persistence.Entity in glassfish/modules/javax.persistence.jar

cip
4 years ago

cookie agreement window does not close when behind a proxy. site is unusable