0

[Solved] Changing my "Java Project" to "Maven Project" and adding dependencies in pom.xml solved my problem! thanks to @Jens

Tried following solutions in this question How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException and i've downloaded JAXB jars and added them in my build path then i've got other errors...

I'm trying to use Hibernate with a Java project using hibernate.cfg.xml. I followed a tutorial and added all required jars, but whenever I run the app, I get a NoClassDefFoundError:

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:87)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:123)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:118)
    at org.studyeasy.hibernate.App.main(App.java:12)

User.java:

package org.studyeasy.hibernate.entity;

import javax.persistence.Column;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "user")
public class User {

    @Id
    @Column(name = "id")
    int id;

    @Column(name = "username")
    String username;

    @Column(name = "password")
    String password;

    @Column(name = "first_name")
    String firstName;

    @Column(name = "last_name")
    String lastName;
    
    public User(String username, String password, String firstName, String lastName) {
        this.username = username;
        this.password = password;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public int getUserId() {
        return id;
    }

    public void setUserId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

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

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

App.java:

package org.studyeasy.hibernate;

import org.hibernate.Session;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.studyeasy.hibernate.entity.User;

public class App {

    public static void main(String[] args) {
        SessionFactory factory = new    Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(User.class)
                .buildSessionFactory();

        Session session = factory.getCurrentSession();

        try {
            // Create object of entity class type
            User user = new User("username", "password", "firstName", "lastName");
            // Start transaction
            session.beginTransaction();
            // Perform operation
            session.save(user);
            // Commit the transaction
            session.getTransaction().commit();
            System.out.println("Row added!");
        } finally {
            session.close();
            factory.close();
        }
    }
}

hibernate.cfg.xml file:

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 
<hibernate-configuration>
    <session-factory>
 
        <!-- Connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
              <!-- Sample MySQL URL provided  -->  
        <property name="connection.url">jdbc:mysql://localhost:3306/project</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
           
        <!-- Show SQL on console -->
        <property name="show_sql">true</property>
 
        <!--Setting Session context model -->
        <property name="current_session_context_class">thread</property>
 
    </session-factory>
</hibernate-configuration>

Full error :

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:87)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:123)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:118)
    at org.studyeasy.hibernate.App.main(App.java:12)
     Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 5 more

I added the jaxb-api-23.1.jar in my lib folder and to build-path, but I get this error:

Jan 05, 2023 12:46:58 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.8.Final}
Jan 05, 2023 12:46:58 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Exception in thread "main" org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number 0 and column 0 in RESOURCE hibernate.cfg.xml. Message: null
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:133)
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:65)
    at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:57)
    at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:258)
    at org.studyeasy.hibernate.App.main(App.java:13)
Caused by: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
 - with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory]
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:278)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:421)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662)
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:122)
    ... 5 more
Caused by: java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:122)
    at javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:155)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:276)
    ... 9 more
I. Riahi
  • 5
  • 2
  • 1
    Do you have `javax.xml` as a dependency? – Jens Jan 05 '23 at 11:48
  • no, should i add the javax.xml to my lib folder and add it in the build path ? – I. Riahi Jan 05 '23 at 11:49
  • You need it. I do not need how you build your project, so i con not duggest wher you have to add it. But I would suggest you to use `maven` or `gradle` for building and dependency management – Jens Jan 05 '23 at 11:51
  • alright thanks, i will see how to convert my project to a maven project properly – I. Riahi Jan 05 '23 at 11:53
  • The last error means that you're missing a JAXB implementation (a.k.a. JAXB runtime) on the runtime classpath. – Mark Rotteveel Jan 05 '23 at 12:22
  • well i solved this problem thanks to @Jens, the problem is that i was following an Hibernate tutorial and he's using a simple "Java Project" with Hibernate for this tuto. I added JAXB jars but i got other errors and so on... Changing my project to Maven project and adding dependencies in pom.xml solved my problem ! – I. Riahi Jan 08 '23 at 18:39

1 Answers1

-1

This error happens when you use java 9 or higher versions of java.

A short history of JAXB on Java from JAXB history The Jakarta XML Binding (JAXB; formerly Java Architecture for XML Binding) is an XML binding framework to convert Java classes to and from XML.

  1. The JAXB is part of Java 6, 7, and 8.
  2. Java 9 deprecated the Java EE modules, including the JAXB packages javax.xml.*.
  3. The JAXB APIs are still deprecated in Java 10.
  4. Java 11 deleted the JAXB APIs javax.xml.* completely.
  5. Oracle submitted the Java EE to Eclipse Foundation and repackaged it to jakarta.xml.* since version 3.0.

Here you find a well defined solution javax/xml/bind/JAXBException error for your problem.

Yas.lbf
  • 24
  • 2
  • I already followed solutions in that question before making this one but i've got other errors and so on... Changing my project to Maven Project and adding the dependencies in pom.xml solved these problems ! thanks to @Jens – I. Riahi Jan 08 '23 at 18:42