Java.lang.NoClassDefFoundError hibernate [复制]

这个问题在这里已有答案:

我对休眠很新 . 我试图用hibernate创建我的第一个应用程序并获得下一个异常:

线程“main”java.lang中的异常 . NoClassDefFoundError:org.hibernate.boot.cfgxml.internal.ConfigLoader上的javax / xml / bind / JAXBException $ 1.initialize(ConfigLoader.java:41)org.hibernate.boot.cfgxml.internal.ConfigLoader $ 1.initialize(ConfigLoader.java) :38)在org.hibernate.boot上的org.hibernate.internal.util.ValueHolder.getValue(ValueHolder.java:55)org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:57) . registry.tandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)位于Main.main(Main.java:19)的org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:152)引起:java.lang.ClassNotFoundException :java.base / jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)中的javax.xml.bind.JAXBException,位于java.base / jdk.internal.loader.ClassLoaders $ AppClassLoader.loadClass(ClassLoaders.java: 185)在java.base / java.lang.ClassLoader.loadClass(ClassLoader.java:496)... 7更多

我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>app</groupId>
    <artifactId>qapp</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.2.Final</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.10</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

主要方法

public static void main(String[] args){
   SessionFactory sessionFactory;
   StandardServiceRegistry serviceRegistry =
            new StandardServiceRegistryBuilder().configure().build();
   Metadata metadata =
            new MetadataSources(serviceRegistry)
            .getMetadataBuilder().build();
        sessionFactory = metadata.getSessionFactoryBuilder().build();
}

回答(1)

3 years ago

您缺少依赖项 . 对于您的具体情况,它是:

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>

您可能会看到其他类似的问题( NoClassDefFoundError ),检查您的依赖项并添加相关的依赖项(如果它缺失) .