title: Exception in thread "main" javax.ejb.EJBException: java.lang.ClassNotFoundException: javax.persistence.PersistenceException
body: <p><strong>I have this trouble and i cannot solve it with JNDI</strong>
  <strong>class ITCenterManagementPersistenceBeanRemote</strong> </p>
  
  <pre><code>public interface ITCenterManagementPersistenceBeanRemote {
  public void insertStudent(long id, String name, String birthday, String address, String phonenumber, String email);
  public void insertStudent(long id, String name, String birthday, String address, String phonenumber, String email);}
  </code></pre>
  
  <p><strong>class ITCenterManagementPersistenceBean</strong></p>
  
  <pre><code>    @Stateless
      public class ITCenterManagementPersistenceBean implements ITCenterManagementPersistenceBeanRemote {
  
          @PersistenceContext(unitName = "QuanLiTTTinHocPU")
          private EntityManager entityManager;
  
          public ITCenterManagementPersistenceBean() {
  
          }
  
          @Override
          public void insertStudent(long id, String name, String birthday, String address, String phonenumber, String email) {
              Student s = new Student(id, name, birthday, address, phonenumber, email);
              entityManager.persist(s);
          }
  @Override
      public List&lt;Student&gt; getStudent() {
          return entityManager.createNamedQuery("Student.findAll").getResultList();
      }}
  </code></pre>
  
  <p><strong>test class</strong></p>
  
  <pre><code>public class ITCenterManagementTester {
  
  private Properties props;
  private InitialContext ctx;
  
  public ITCenterManagementTester() {
      readJNDI();
  }
  
  private void readJNDI() {
      props = new Properties();
  
      try {
          props.load(new FileInputStream("jndi.properties"));
      } catch (IOException e) {
          System.err.println(e.toString());
      }
  
      try {
          ctx = new InitialContext(props);
          //ctx.close();
      } catch (NamingException ex) {
          System.err.println(ex.toString());
      }
  }
  
  private String getJNDI() {
      String appName = "";
      String moduleName = "QuanLiTTTinHoc";
      String distinctName = "";
      String sessionBeanName = ITCenterManagementPersistenceBean.class.getSimpleName();
      String viewClassName = ITCenterManagementPersistenceBeanRemote.class.getName();
  
      return "ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + sessionBeanName + "!" + viewClassName;
  }
  
  private void showGUI() {
      System.out.println("\n===============================");
      System.out.println("Welcome to IT center Management");
      System.out.println("===============================");
      System.out.print("Options: \n1. student \n2. List All student \n3. Teacher \n4. List all Tacher"
              + "\n5. Staff \n6. List all Staff \n7. Add class \n8. List of class\n9. Add Subject"
              + "\n10. Add Class Shift \n11. Class Shift list \n12. Add Prerequisite Subject \n13. Add Schedule \n14. Add Exam Schedule"
              + "\n15. Add Result \n16. Addtendance \n17. Teaching Schedule \n18. Add Register Information"
              + "\n19. List of Register Information\n20. Exit \nEnter Choice: ");
  }
  
  /*private void getAllBooks() {
      try {
          // We can define another bean to access the LibrarySessionBeanRemote
          ITCenterManagementPersistenceBeanRemote CenterBean2 = (ITCenterManagementPersistenceBeanRemote) ctx.lookup(getJNDI());
          //List&lt;String&gt; studentList = CenterBean2.getStudent();
  
          // Print all books
          System.out.println("\n===============================");
          System.out.println("Listing Student");
          System.out.println("===============================");
          //for (int i = 0; i &lt; studentList.size(); i++) {
          //    System.out.println(i + "\t" + studentList.get(i));
          //}
          System.out.println("");
  
      } catch (NamingException ex) {
          System.err.println(ex.toString());
      }
  }*/
  public void testStatelessEJB() {
      try {
          // Scanner definition
          Scanner sc = new Scanner(System.in);
  
          // ITCenterManagementPersistenceBeanRemote
          ITCenterManagementPersistenceBeanRemote session = (ITCenterManagementPersistenceBeanRemote) ctx.lookup(getJNDI());
  
          int choice = 0;
          long studentID;
          String name;
          String birthday;
          String address;
          String phonenumber;
          String email;
          Subject subjectName;
          long classID;
          String className;
          Date beginDay;
          Date endDay;
          BigInteger stdNumber;
          BigInteger fee;
          long shiftID;
          String shiftName;
          String beginTime;
          String endTime;
          double point;
          long PSubjectID;
          boolean absent;
          Date absentDate;
          Staff staffID;
          long registerID;
          Date registerDate;
          ClassShift ShiftID;
          Date testDate;
          String time;
          BigInteger hour;
          String room;
          do {
              showGUI();
              choice = Integer.parseInt(sc.nextLine());
              switch (choice) {
                  case 1:
                      System.out.print("insert Student ID: ");
                      studentID = Long.parseLong(sc.nextLine()) ;
                      System.out.print("insert Student name: ");
                      name = sc.nextLine();
                      System.out.print("insert Student birthday: ");
                      birthday = sc.nextLine();
                      System.out.print("insert Student address: ");
                      address = sc.nextLine();
                      System.out.print("insert Student phone number: ");
                      phonenumber = sc.nextLine();
                      System.out.print("insert Student email: ");
                      email = sc.nextLine();
                      session.insertStudent(studentID, name, birthday, address, phonenumber, email);
                      System.out.println("Done!");
                      break;
                  case 2:
                      List&lt;Student&gt; s = session.getStudent();
                      if (s.isEmpty()) {
                          System.out.println("There is no student in list!");
                      }
                      System.out.println("\n=========================");
                      System.out.println("Listing Student");
                      System.out.println("=========================");
                      for(int i = 0; i&lt;s.size();i++){
                          System.out.println((i+1)+"\t"+s.get(i));
                      }
                      System.out.println();
  
                      break;
  
                  default:
                      break;
  
              }
          } while (choice != 50);
  
      } catch (NamingException ex) {
          System.err.println(ex.toString());
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>**</p>
  
  <blockquote>
    <p>this is class student</p>
  </blockquote>
  
  <p>**</p>
  
  <pre><code>package edu.tdt.persistence;
  
  import java.io.Serializable;
  import java.util.Collection;
  import javax.persistence.Basic;
  import javax.persistence.CascadeType;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.Id;
  import javax.persistence.NamedQueries;
  import javax.persistence.NamedQuery;
  import javax.persistence.OneToMany;
  import javax.persistence.Table;
  import javax.validation.constraints.NotNull;
  import javax.validation.constraints.Size;
  import javax.xml.bind.annotation.XmlRootElement;
  import javax.xml.bind.annotation.XmlTransient;
  
  /**
   *
   * @author MSI
   */
  @Entity
  @Table(name = "Student", catalog = "ITCenterManagement", schema = "public")
  @XmlRootElement
  @NamedQueries({
      @NamedQuery(name = "Student.findAll", query = "SELECT s FROM Student s")
      , @NamedQuery(name = "Student.findByStudentID", query = "SELECT s FROM Student s WHERE s.studentID = :studentID")
      , @NamedQuery(name = "Student.findByName", query = "SELECT s FROM Student s WHERE s.name = :name")
      , @NamedQuery(name = "Student.findByBirthDay", query = "SELECT s FROM Student s WHERE s.birthDay = :birthDay")
      , @NamedQuery(name = "Student.findByAddress", query = "SELECT s FROM Student s WHERE s.address = :address")
      , @NamedQuery(name = "Student.findByPhoneNumber", query = "SELECT s FROM Student s WHERE s.phoneNumber = :phoneNumber")
      , @NamedQuery(name = "Student.findByEmail", query = "SELECT s FROM Student s WHERE s.email = :email")
      , @NamedQuery(name = "Student.findByA", query = "SELECT s FROM Student s WHERE s.a = :a")})
  public class Student implements Serializable {
  
      @Size(max = 2147483647)
      @Column(name = "Birthday", length = 2147483647)
      private String birthday;
      @Size(max = 2147483647)
      @Column(name = "PhoneNumber", length = 2147483647)
      private String phoneNumber;
  
      private static final long serialVersionUID = 1L;
      @Id
      @Basic(optional = false)
      @NotNull
      @Column(name = "StudentID", nullable = false)
      private Long studentID;
      @Size(max = 2147483647)
      @Column(name = "Name", length = 2147483647)
      private String name;
      @Size(max = 2147483647)
      @Column(name = "BirthDay", length = 2147483647)
      private String birthDay;
      @Size(max = 2147483647)
      @Column(name = "Address", length = 2147483647)
      private String address;
      // @Pattern(regexp="[a-z0-9!#$%&amp;'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&amp;'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
      @Size(max = 2147483647)
      @Column(name = "Email", length = 2147483647)
      private String email;
      @Size(max = 2147483647)
      @Column(name = "", length = 2147483647)
      private String a;
      @OneToMany(cascade = CascadeType.ALL, mappedBy = "student")
      private Collection&lt;Attendance&gt; attendanceCollection;
      @OneToMany(mappedBy = "studentID")
      private Collection&lt;RegisterInformation&gt; registerInformationCollection;
      @OneToMany(cascade = CascadeType.ALL, mappedBy = "student")
      private Collection&lt;TestResult&gt; testResultCollection;
  
      public Student() {
      }
  
      public Student(long studentID) {
          this.studentID = studentID;
  
      }
  
      public Student(long studentID, String name, String birthday, String address, String phonenumber, String email) {
          this.studentID = studentID;
          this.name = name;
          this.birthDay = birthday;
          this.address = address;
          this.phoneNumber = phonenumber;
          this.email = email;
      }
  
      public Long getStudentID() {
          return studentID;
      }
  
      public void setStudentID(Long studentID) {
          this.studentID = studentID;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getBirthDay() {
          return birthDay;
      }
  
      public void setBirthDay(String birthDay) {
          this.birthDay = birthDay;
      }
  
      public String getAddress() {
          return address;
      }
  
      public void setAddress(String address) {
          this.address = address;
      }
  
      public String getEmail() {
          return email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  
      public String getA() {
          return a;
      }
  
      public void setA(String a) {
          this.a = a;
      }
  
      @XmlTransient
      public Collection&lt;Attendance&gt; getAttendanceCollection() {
          return attendanceCollection;
      }
  
      public void setAttendanceCollection(Collection&lt;Attendance&gt; attendanceCollection) {
          this.attendanceCollection = attendanceCollection;
      }
  
      @XmlTransient
      public Collection&lt;RegisterInformation&gt; getRegisterInformationCollection() {
          return registerInformationCollection;
      }
  
      public void setRegisterInformationCollection(Collection&lt;RegisterInformation&gt; registerInformationCollection) {
          this.registerInformationCollection = registerInformationCollection;
      }
  
      @XmlTransient
      public Collection&lt;TestResult&gt; getTestResultCollection() {
          return testResultCollection;
      }
  
      public void setTestResultCollection(Collection&lt;TestResult&gt; testResultCollection) {
          this.testResultCollection = testResultCollection;
      }
  
      @Override
      public int hashCode() {
          int hash = 0;
          hash += (studentID != null ? studentID.hashCode() : 0);
          return hash;
      }
  
      @Override
      public boolean equals(Object object) {
          // TODO: Warning - this method won't work in the case the id fields are not set
          if (!(object instanceof Student)) {
              return false;
          }
          Student other = (Student) object;
          if ((this.studentID == null &amp;&amp; other.studentID != null) || (this.studentID != null &amp;&amp; !this.studentID.equals(other.studentID))) {
              return false;
          }
          return true;
      }
  
      @Override
      public String toString() {
          return "edu.tdt.persistence.Student[ studentID=" + studentID + " ]";
      }
  
      public String getBirthday() {
          return birthday;
      }
  
      public void setBirthday(String birthday) {
          this.birthday = birthday;
      }
  
      public String getPhoneNumber() {
          return phoneNumber;
      }
  
      public void setPhoneNumber(String phoneNumber) {
          this.phoneNumber = phoneNumber;
      }
  
  }
  </code></pre>
  
  <p>main</p>
  
  <pre><code>public class QuanLiTTTinHocTester {
  
      /**
       * @param args the command line arguments
       */
      public static void main(String[] args) throws EJBException, IllegalStateException{
          // TODO code application logic here
          ITCenterManagementTester test = new ITCenterManagementTester();
          test.testStatelessEJB();
      }
  
  }
  </code></pre>
  
  <p>when i run main and i recieve this error</p>
  
  <pre><code>    Exception in thread "main" javax.ejb.EJBException: java.lang.ClassNotFoundException: javax.persistence.PersistenceException
      at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:238)
      at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:183)
      at org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:146)
      at com.sun.proxy.$Proxy2.getStudent(Unknown Source)
      at edu.tdt.test.ITCenterManagementTester.testStatelessEJB(ITCenterManagementTester.java:153)
      at edu.tdt.test.QuanLiTTTinHocTester.main(QuanLiTTTinHocTester.java:20)
  Caused by: java.lang.ClassNotFoundException: javax.persistence.PersistenceException
      at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:348)
      at org.jboss.marshalling.AbstractClassResolver.loadClass(AbstractClassResolver.java:131)
      at org.jboss.marshalling.AbstractClassResolver.resolveClass(AbstractClassResolver.java:112)
      at org.jboss.marshalling.river.RiverUnmarshaller.doReadClassDescriptor(RiverUnmarshaller.java:948)
      at org.jboss.marshalling.river.RiverUnmarshaller.doReadNewObject(RiverUnmarshaller.java:1255)
      at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:276)
      at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:224)
      at org.jboss.marshalling.river.RiverUnmarshaller.readFields(RiverUnmarshaller.java:1745)
      at org.jboss.marshalling.river.RiverObjectInputStream.defaultReadObject(RiverObjectInputStream.java:81)
      at java.lang.Throwable.readObject(Throwable.java:914)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.jboss.marshalling.reflect.SerializableClass.callReadObject(SerializableClass.java:307)
      at org.jboss.marshalling.river.RiverUnmarshaller.doInitSerializable(RiverUnmarshaller.java:1637)
      at org.jboss.marshalling.river.RiverUnmarshaller.doInitSerializable(RiverUnmarshaller.java:1606)
      at org.jboss.marshalling.river.RiverUnmarshaller.doInitSerializable(RiverUnmarshaller.java:1606)
      at org.jboss.marshalling.river.RiverUnmarshaller.doInitSerializable(RiverUnmarshaller.java:1606)
      at org.jboss.marshalling.river.RiverUnmarshaller.doReadNewObject(RiverUnmarshaller.java:1285)
      at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:276)
      at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:209)
      at org.jboss.marshalling.AbstractObjectInput.readObject(AbstractObjectInput.java:41)
      at org.jboss.ejb.client.remoting.InvocationExceptionResponseHandler$MethodInvocationExceptionResultProducer.getResult(InvocationExceptionResponseHandler.java:79)
      at org.jboss.ejb.client.EJBClientInvocationContext.getResult(EJBClientInvocationContext.java:276)
      at org.jboss.ejb.client.EJBObjectInterceptor.handleInvocationResult(EJBObjectInterceptor.java:64)
      at org.jboss.ejb.client.EJBClientInvocationContext.getResult(EJBClientInvocationContext.java:290)
      at org.jboss.ejb.client.EJBHomeInterceptor.handleInvocationResult(EJBHomeInterceptor.java:88)
      at org.jboss.ejb.client.EJBClientInvocationContext.getResult(EJBClientInvocationContext.java:290)
      at org.jboss.ejb.client.TransactionInterceptor.handleInvocationResult(TransactionInterceptor.java:46)
      at org.jboss.ejb.client.EJBClientInvocationContext.getResult(EJBClientInvocationContext.java:290)
      at org.jboss.ejb.client.ReceiverInterceptor.handleInvocationResult(ReceiverInterceptor.java:142)
      at org.jboss.ejb.client.EJBClientInvocationContext.getResult(EJBClientInvocationContext.java:265)
      at org.jboss.ejb.client.EJBClientInvocationContext.awaitResponse(EJBClientInvocationContext.java:453)
      at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:204)
      ... 5 more
  C:\Users\MSI\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
  BUILD FAILED (total time: 4 seconds)
  </code></pre>
  
  <p>*
  i implement student class form postgresql and so do the other but i just example with the student class first. when i run test project and it show the error but i don't know how to solve.</p>
  
  <blockquote>
    <p>i don't know why, so please help!</p>
  </blockquote>
  
  <p>*</p>