0

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", "ls@bk.com");

            // 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.

BaSic
  • 113
  • 2
  • 9

5 Answers5

3

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.

Little Pie
  • 31
  • 2
1

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>
Jens
  • 67,715
  • 15
  • 98
  • 113
  • I'm not using maven. – BaSic Oct 12 '17 at 13:10
  • @BaSic Then download it and add it to your classpath – Jens Oct 12 '17 at 13:10
  • New error: Exception in thread "main" java.lang.NoSuchMethodError: javax.xml.bind.JAXBContext.newInstance([Ljava/lang/Class;)Ljavax/xml/bind/JAXBContext; – BaSic Oct 12 '17 at 13:12
  • @BaSic ok then is is the wron Version. try a newer one – Jens Oct 12 '17 at 13:14
  • Error again 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 – BaSic Oct 12 '17 at 14:26
  • i guess your `hibernate.cfg.xml` file is wrong or not found or not set. It is not possible to see in the shared code – Jens Oct 12 '17 at 14:33
1

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

Sounak Saha
  • 852
  • 1
  • 9
  • 21
  • 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 – BaSic Oct 12 '17 at 13:41
0

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

Thomas
  • 1
  • 1
  • Do you intend this to be an answer according to [answer]? If not please delete it. – Yunnosch Mar 01 '22 at 07:11
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 01 '22 at 12:20
-2

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

BaSic
  • 113
  • 2
  • 9
  • Not really a fix, please see https://stackoverflow.com/questions/43574426/how-to-resolve-java-lang-noclassdeffounderror-javax-xml-bind-jaxbexception-in-j – Bruno Medeiros Mar 06 '19 at 15:41