gpt4 book ai didi

java.lang.ClassNotFoundException : org. dom4j.DocumentException 异常

转载 作者:行者123 更新时间:2023-11-29 09:38:01 24 4
gpt4 key购买 nike

我编写了一些代码来学习 hibernate 。它抛出了下面的错误。我如何找出问题所在并加以解决? dom4j 听起来像是一个 XML 问题。 hibernate.cfg.xml 是问题所在?

Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentException
at com.examscam.model.User.persist(User.java:45)
at com.examscam.model.User.main(User.java:57)
Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException
at java.net.URLClassLoader$1.run(Unknown Source)
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)
... 2 more

(为什么 eclipse 不显示这个“2more”的东西?)

代码-

package com.examscam.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 {

private Long id;
private String password;

@Id
@GeneratedValue

public Long getId(){
return id;

}

public void setId(Long id){

Class c1 = User.class;
this.id = id;

}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public static void persist(){

/*
* Contains all the info needed to connect to database, along with
* info from your JPA annotated class, ie this class.
*/
AnnotationConfiguration config = new AnnotationConfiguration();
config.addAnnotatedClass(User.class);
config.configure();

SchemaExport schemaExport = new SchemaExport(config);
schemaExport.create(true, true);

}


public static void main(String[]args){

persist();

}

}

XML 文件 -

<?xml version = '1.0' encoding='UTF-8' ?>

<!Doctype hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>

<property name = "connection.url">
jdbc:mysql://localhost/examscam
</property>

<property name = "connection.username">
root
</property>

<property name = "connection.password">
password
</property>

<property name = "connection.driver_class">
com.mysql.jdbc.Driver
</property>

<property name = "dialect">
org.hibernate.dialect.MySQLDialect
</property>

<property name = "transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory
</property>

<property name = "current_session_context_class">
thread
</property>

<!-- this will show us all the SQL statements-->

<property name = "hibernate.show_sql">
true
</property>

<!-- We dont use any mapping files as of now-->

</session-factory>
</hibernate-configuration>

更新

修复上面的错误后,又出现了新的问题——

我正在用 Eclipse 编写代码。我添加了代码所需的所有可能的 jar,但我的代码仍然无法正常工作。当我已经添加 sl4fj (slf4j-api-1.5.8.jar) 构建路径 ?

错误-

  SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j
/impl/StaticLoggerBinder
at org.slf4j.LoggerFactory.getSingleton(LoggerFactory.java:223)
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:120)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:111)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:269)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:242)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:255)
at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:152)
at com.examscam.model.User.persist(User.java:45)
at com.examscam.model.User.main(User.java:57)
Caused by: java.lang.ClassNotFoundException: org.slf4j.impl.StaticLoggerBinder
at java.net.URLClassLoader$1.run(Unknown Source)
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)
... 9 more

最佳答案

确保 dom4j.jar 在类路径中。

2more 你在说什么。

当您看到“...2 more”时,这意味着“由...引起”异常的剩余行与父异常从该点开始的剩余行相同。

关于java.lang.ClassNotFoundException : org. dom4j.DocumentException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16051267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com