我读过多篇关于NoClassDefFoundError的帖子,但没有找到令人满意的答案。
我开始学习hibernate。我编写了简单的应用程序来保存我的数据库中的Movie对象。
我成功地使用
编译了我的代码javac -classpath ~/cp/hibernate-release-5.0.1.Final/lib/required/hibernate-core-5.0.1.Final.jar -d classes justhibernate/BasicMovieManager.java justhibernate/Movie.java
但是,当我尝试使用
运行我的代码时java -classpath ./:~/cp/hibernate-release-5.0.1.Final/lib/required/hibernate-core-5.0.1.Final.jar:./classes/ justhibernate.BasicMovieManager
我遇到了错误
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/service/ServiceRegistry
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.hibernate.service.ServiceRegistry
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
我在hibernate.cfg.xml
目录中有Movie.hbm.xml
和./
个文件。这是我的代码:
BasicMovieManager.java
package justhibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.MetadataSources;
public class BasicMovieManager
{
private SessionFactory sessionFactory = null;
private void setUp() throws Exception
{
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure()
.build();
try
{
sessionFactory = new MetadataSources( registry )
.buildMetadata().buildSessionFactory();
}
catch (Exception e)
{
StandardServiceRegistryBuilder.destroy( registry );
}
}
private void persistMovie(Movie movie)
{
if(sessionFactory == null)
{
try {setUp();}
catch(Exception e){e.printStackTrace();}
}
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(movie);
session.getTransaction().commit();
session.close();
}
public static void main(String[] args)
{
BasicMovieManager bmm = new BasicMovieManager();
Movie m = new Movie();
m.setId(2);
m.setTitle("Harry Potter and the Chamber of Secrets");
m.setDirector("Steve Kloves");
m.setSynopsis("2nd part od thunder-face magician");
bmm.persistMovie(m);
}
}
Movie.java
package justhibernate;
public class Movie
{
private int id = 0;
private String title = null;
private String synopsis = null;
private String director = null;
public void setId(int id)
{
this.id = id;
}
public int getId()
{
return id;
}
public void setTitle(String title)
{
this.title = title;
}
public String getTitle()
{
return title;
}
public void setSynopsis(String synopsis)
{
this.synopsis = synopsis;
}
public String getSynopsis()
{
return synopsis;
}
public void setDirector(String director)
{
this.director = director;
}
public String getDirector()
{
return director;
}
}
的hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.username">
user
</property>
<property name="connection.password">
password
</property>
<property name="dialect">
org.hibernate.dialect.MySQL5Dialect
</property>
<mapping resource="Movie.hbm.xml" />
</session-factory>
</hibernate-configuration>
Movie.hbm.xml
<hibernate-mapping>
<class name="justhibernate.Movie" table="MOVIES">
<id name="id" column="ID">
<generator class="native" />
</id>
<property name="title" column="TITLE" />
<property name="director" column="DIRECTOR" />
<property name="synopsis" column="SYNOPSIS" />
</class>
</hibernate-mapping>
答案 0 :(得分:0)
看起来它是由Hibernate库的相对(以home符号〜开头)路径引起的。我试图用绝对路径运行你的代码,它成功找到了Hibernate。但请注意,如果没有指定MySQL JDBC,JBoss日志记录和其他必需库的路径,您的代码将无法运行,完整列表为here。