简体   繁体   中英

Instantiating a hibernate Configuration throws error: Exception in thread “main” java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

I am fairly new to the hibernate framework and I'm creating a simple application with a Udemy course. I've continually been getting a 'java.lang.NoClassDefFoundError' on the following stack. It appears that when I create a org.hibernate.cfg.Configuration object the exception is thrown. Any guidance would be appreciated on how to solve the following issue, is it possible that this hibernate version is buggy and I need to backtrack to a previous version?

Hibernate-Core Version: 5.3.0.Final

Hibernate-Annotations: 3.5.6.Final

My-SQL Server Version: 8.0.12

DEBUG - Logging Provider: org.jboss.logging.Log4jLoggerProvider
DEBUG - Adding Integrator [org.hibernate.cfg.beanvalidation.BeanValidationIntegrator].
DEBUG - Adding Integrator [org.hibernate.secure.spi.JaccIntegrator].
DEBUG - Adding Integrator [org.hibernate.cache.internal.CollectionCacheInvalidator].
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
    at org.hibernate.boot.spi.XmlMappingBinderAccess.<init>(XmlMappingBinderAccess.java:43)
    at org.hibernate.boot.MetadataSources.<init>(MetadataSources.java:86)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:123)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:118)
    at com.dataPack.data.HibernateUtil.buildSessionFactory(HibernateUtil.java:16)
    at com.dataPack.data.HibernateUtil.<clinit>(HibernateUtil.java:10)
    at com.dataPack.data.Application.main(Application.java:9)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
    ... 7 more

Here is the HibernateUtil class I created to build the sessionFactory.

package com.dataPack.data;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;


public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {

        Configuration configuration = null;
        try {
            configuration = new Configuration();

            return configuration
                    .buildSessionFactory(new StandardServiceRegistryBuilder()
                            .applySettings(configuration.getProperties())
                                .build());

        } catch(Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Issue Building Session Factory!");
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

Here is the hibernate.properties file we are suppose to use.

hibernate.connection.username=user
hibernate.connection.password=password
hibernate.connection.url=jdbc:mysql://localhost:3306/ifinances
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

Since this project is known to work, it's likely that others have used a different Java version as the jaxb Apis were removed from Java SE. There are multiple ways to address this (as detailed in How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException in Java 9 ) but the most reliable is to add the jaxb API dependency (groupId javax.xml.bind, artifactId jaxb-api - https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api/2.3.0 ) to the pom.xml or gradle build file.

Then rebuild and if you still hit ClassNotFound errors then see https://stackoverflow.com/a/43574427/9705485

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM