【发布时间】:2015-09-23 14:00:52
【问题描述】:
我已经阅读了多篇关于 NoClassDefFoundError 的帖子,但没有找到令人满意的答案。
我已经开始学习休眠了。我已经编写了简单的应用程序来将 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);
}
}
电影.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>
电影.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>
【问题讨论】:
-
我在开头添加了完整的 javac 和 java 命令。它们包含相同的 .jar 文件。
-
尝试使用 Eclipse 或 IntelliJ 或一些 IDE,它们通常会处理您可能错过的事情。
-
我知道使用 IDE 更简单,但我想确保我可以从 shell 编译和运行它。
-
Java 在运行代码时对波浪号运算符一无所知(如 John Snow ;)),在下面回答,还发现了这个主题 stackoverflow.com/questions/1779178/… 和 stackoverflow.com/questions/9122989/…
标签: java hibernate noclassdeffounderror