There is probably something I overlooked in doing this simple example. I am doing a simple Hibernate example and for some reason I am getting a ‘ClassNotFoundException: org.hibernate.cfg.Configuration’ error when trying to run the following code in my main method:
AnnotationConfiguration config = new AnnotationConfiguration();
config.addAnnotatedClass(User.class);
config.configure();
new SchemaExport(config).create(true, true);
Why would I be getting a ClassNotFoundException when I clearly declare the hibernate dependency in my build.gradle?
My project is very simple with just a few files. It is on GitHub at this link.
Full Error Log
Exception in thread “main” java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2688)
at java.lang.Class.privateGetMethodRecursive(Class.java:3035)
at java.lang.Class.getMethod0(Class.java:3005)
at java.lang.Class.getMethod(Class.java:1771)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
… 7 more
build.gradle
apply plugin: ‘java’
apply plugin: ‘eclipse’
sourceCompatibility = 1.8
version = ‘0.1’
jar {
baseName = ‘hibernatemadeeasy’
version = ‘0.0.1-SNAPSHOT’
manifest {
attributes ‘Main-Class’: ‘org.gradle.model.User’
}
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
// compile group: ‘commons-collections’, name: ‘commons-collections’, version: ‘3.2’
compile(‘org.hibernate:hibernate-core:4.3.6.Final’)
compile(‘org.hibernate:hibernate-annotations:3.5.6-Final’)
compile(‘org.slf4j:slf4j-log4j12:1.7.7’)
runtime(‘org.postgresql:postgresql:9.3-1102-jdbc41’)
testCompile group: ‘junit’, name: ‘junit’, version: ‘4.+’
}
test {
systemProperties ‘property’: ‘value’
}
uploadArchives {
repositories {
flatDir {
dirs ‘repos’
}
}
}
User class
package org.gradle.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
@Entity
public class User {
@Id
@GeneratedValue
private Long id;
private String password;
public Long getId() {
return id;
}
public String getPassword() {
return password;
}
public void setId(Long id) {
this.id = id;
}
public void setPassword(String password) {
this.password = password;
}
public static void main(String[] args) {
AnnotationConfiguration config = new AnnotationConfiguration();
config.addAnnotatedClass(User.class);
config.configure();
new SchemaExport(config).create(true, true);
}
}