In Eclipse I made a project with DAO and it was working fine. I then I made a new JPA project and added it in the Deployment Assembly of the initial project. I than converted one DAO to work with JPA and put it in the JPA project. When i tested the application it gave an Java error NoClassDefFoundError (see bottom). I have no idea why. I have checked everywhere but i can’t find the error. The JPA is called in the servlet:
package eshop;
....
import model.ProductModelDAO;
import model.ProductModelDAOImpl;
ProductModelDAO dao4 = new ProductModelDAOImpl("Product");
ArrayList products = dao4.getProductsByCategory(categoryId);
Then in the JPA project I have Product.java which is generated from the “products” table. I have the persistence.xml. And i have the two original DAO’s:
package model;
import java.util.*;
public interface ProductModelDAO {
public ArrayList searchProducts(String keyword);
public ArrayList getProductsByCategory(String categoryId);
public Product getProductById(String productID);
}
and:
package model;
import java.util.ArrayList;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
public class ProductModelDAOImpl implements ProductModelDAO {
@PersistenceContext
private EntityManager em;
// this is the default constructor
public ProductModelDAOImpl()
{
this("Product");
}
public ProductModelDAOImpl(String unitName)
{
EntityManagerFactory emf = Persistence.createEntityManagerFactory(unitName);
em = emf.createEntityManager();
}
// this is the special constructor to set the entity manager.
// this is used when we run with EJB3
public ProductModelDAOImpl(EntityManager em)
{
this.em = em;
}
public void setEntityManager(EntityManager em)
{
this.em = em;
}
@Override
public ArrayList<Product> searchProducts(String keyword) {
@SuppressWarnings("unchecked")
ArrayList<Product> results = (ArrayList<Product>)
em.createQuery("select A from products A where (A.product_name = ?1 or A.descr = ?1)")
.setParameter(1, keyword)
.getResultList();
return results;
}
@Override
public ArrayList <Product> getProductsByCategory(String categoryId) {
@SuppressWarnings("unchecked")
ArrayList<Product> results = (ArrayList<Product>)
em.createQuery("select A from products A where A.category_id = ?1")
.setParameter(1, categoryId)
.getResultList();
return results;
}
@Override
public Product getProductById(String productID) {
Product results =
(Product) em.createQuery("select A from products A where A.product_id = ?1")
.setParameter(1, productID)
.getResultList();
return results;
}
}
The persistence xml is the following:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="abook">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>model.Product</class>
<properties>
<property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver" />
<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/shop" />
<property name="openjpa.ConnectionUserName" value="root" />
<property name="openjpa.ConnectionPassword" value="" />
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" />
</properties>
</persistence-unit>
</persistence>
The error is the following
java.lang.NoClassDefFoundError: model/ProductModelDAOImpl
at eshop.Servlet.doPost(Servlet.java:112)
at eshop.Servlet.doGet(Servlet.java:57)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:300)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:183)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.doIt(WebAppServletContext.java:3686)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3650)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2268)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2174)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1446)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)
NoClassDefFoundError in Java comes when Java Virtual Machine is not able to find a particular class at runtime which was available during compile time. See if
ProductModelDAOImplis available in the web-app classpath.Read more here