BaSic
BaSic

Reputation: 113

Hibernate Session Factory javax/xml/bind/JAXBException error

I'm trying to make some kind of simple java + hibernate app. Somehow when i run the app there is an error.

CreateStudentDemo.java

package com.basicproject.hibernate.demo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.basicproject.hibernate.demo.entity.Student;
public class CreateStudentDemo {

    public static void main(String[] args) {


        // create session factory
        SessionFactory factory = new Configuration()
                .configure("hibernate.cfg.xml")
                .addAnnotatedClass(Student.class)
                .buildSessionFactory();


        // create session
        Session session = factory.getCurrentSession();

        try {
            // create a student object
            System.out.println("Creating new student object...");
            Student tempStudent = new Student("Luke", "Samson", "[email protected]");

            // start a transaction
            session.beginTransaction();

            // save the student object
            System.out.println("Saving the student...");
            session.save(tempStudent);

            // commit transaction
            session.getTransaction().commit();

            System.out.println("Done!");
        }
        finally {
            factory.close();
        }
    }

}

Student.java

package com.basicproject.hibernate.demo.entity;

import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table;

@Entity @Table(name="student") public class Student {

    @Id     @GeneratedValue(strategy=GenerationType.IDENTITY)   @Column(name="id")  private int id;

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

    @Column(name="last_name")   private String lastName;

    @Column(name="email")   private String email;

    public Student() {

    }

    public Student(String firstName, String lastName, String email) {       this.firstName = firstName;         this.lastName = lastName;       this.email
= email;    }

    public int getId() {        return id;  }

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

    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;   }

    public String getEmail() {      return email;   }

    public void setEmail(String email) {        this.email = email;     }

    @Override   public String toString() {      return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";  }

}

And the error. It seems like i'm missing something but i can't realise that is it yet.

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 com.basicproject.hibernate.demo.CreateStudentDemo.main(CreateStudentDemo.java:14) 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)

Hopefully it can help resolve my problem. Thank You for help. There is also a lib folder in my project. Contains Hibernate's libs(required) and mysql connector.

Upvotes: 0

Views: 11452

Answers (5)

Thomas
Thomas

Reputation: 1

Please change the JRE from the STS/Eclipse to 8 version.
Don't use JRE 1.8 in JDK 16.

Upvotes: 0

Little Pie
Little Pie

Reputation: 31

I think you're attending the same online course as me! In previous lessons the instructor posted a solution to this problem :

This happens because of Java 9 and higher.

Java 9 and higher has removed java.xml.bind from its default classpath. That's why we get the class not found exception. We have to explicitly add JAR files to the build path.


Solution

For Java 9 and higher, you need to additional JAR files.

You need to download the following JAR files:

  • javax.activation-1.2.0.jar
  • jaxb-api-2.3.0.jar
  • jaxb-core-2.3.0.jar
  • jaxb-impl-2.3.0.jar

  1. Download the files using links below:

http://search.maven.org/remotecontent?filepath=com/sun/activation/javax.activation/1.2.0/javax.activation-1.2.0.jar

http://search.maven.org/remotecontent?filepath=javax/xml/bind/jaxb-api/2.3.0/jaxb-api-2.3.0.jar

http://search.maven.org/remotecontent?filepath=com/sun/xml/bind/jaxb-core/2.3.0/jaxb-core-2.3.0.jar

http://search.maven.org/remotecontent?filepath=com/sun/xml/bind/jaxb-impl/2.3.0/jaxb-impl-2.3.0.jar


  1. Copy the JAR files to the lib folder of your project
  • javax.activation-1.2.0.jar
  • jaxb-api-2.3.0.jar
  • jaxb-core-2.3.0.jar
  • jaxb-impl-2.3.0.jar

Use the following steps to add the JAR files to your Java Build Path.

  1. Right-click your project, select Properties

  2. On left-hand side, click Java Build Path

  3. In top-center of dialog, click Libraries

  4. Click Classpath and then Click Add JARs ...

  5. Navigate to the JAR files /lib

Select the files: javax.activation-1.2.0.jar jaxb-api-2.3.0.jar jaxb-core-2.3.0.jar jaxb-impl-2.3.0.jar

  1. Click OK then click Apply and Close

Eclipse will perform a rebuild of your project and it will resolve the related build/runtime errors.

Upvotes: 3

BaSic
BaSic

Reputation: 113

Changed to java 8 instead of java 9 and working perfectly fine.

Upvotes: -2

Sounak Saha
Sounak Saha

Reputation: 943

Please add the below jar in your project classpath. http://central.maven.org/maven2/javax/xml/bind/jaxb-api/2.2.11/jaxb-api-2.2.11.jar

Upvotes: 1

Jens
Jens

Reputation: 69495

You miss jaxb-api-1.0.jar in your classpath.

if you are maven add the following dependency to your code:

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

Upvotes: 1

Related Questions