Java.lang.NoClassDefFoundError hibernate

时间:2017-10-29 13:21:07

标签: java hibernate maven jaxb

我对hibernate很新。我试图用hibernate制作我的第一个应用程序并获得下一个例外:

  

线程中的异常" main" java.lang中。

     

NoClassDefFoundError的:   javax / xml / bind / JAXBException at   org.hibernate.boot.cfgxml.internal.ConfigLoader $ 1.initialize(ConfigLoader.java:41)     在   org.hibernate.boot.cfgxml.internal.ConfigLoader $ 1.initialize(ConfigLoader.java:38)     在   org.hibernate.internal.util.ValueHolder.getValue(ValueHolder.java:55)     在   org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:57)     在   org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)     在   org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:152)     在Main.main(Main.java:19)引起:   java.lang.ClassNotFoundException:javax.xml.bind.JAXBException at   java.base / jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)     在   java.base / jdk.internal.loader.ClassLoaders $ AppClassLoader.loadClass(ClassLoaders.java:185)     at 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 个答案:

答案 0 :(得分:1)

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

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

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