1

I'm creating a simple Web app and decided to use a Spring JdbcTemplate class. But now I'm encountering the NoClassDefFoundError: org/springframework/jdbc/core/RowMapper .

According to the stack trace something is wrong with ControllerServlet on line 32. Which is:

productDAOImpl = new ProductDAOImpl(dataSource);

What may be the reason for the error? What should I do to fix it?

Context Path:/webstore_war_exploded
Servlet Path:/ControllerServlet
Path Info:null
Query String:null
Stack Trace
java.lang.NoClassDefFoundError: org/springframework/jdbc/core/RowMapper
com.ncproject.webstore.controller.ControllerServlet.init(ControllerServlet.java:32)
javax.servlet.GenericServlet.init(GenericServlet.java:244)
io.undertow.servlet.core.LifecyleInterceptorInvocation.proceed(LifecyleInterceptorInvocation.java:117)
org.wildfly.extension.undertow.security.RunAsLifecycleInterceptor.init(RunAsLifecycleInterceptor.java:78)
io.undertow.servlet.core.LifecyleInterceptorInvocation.proceed(LifecyleInterceptorInvocation.java:103)
io.undertow.servlet.core.ManagedServlet$DefaultInstanceStrategy.start(ManagedServlet.java:250)
io.undertow.servlet.core.ManagedServlet.getServlet(ManagedServlet.java:171)
io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:84)
io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:805)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)

ControllerServlet.java

@WebServlet("/ControllerServlet")
public class ControllerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

private ProductDAOImpl productDAOImpl;

@Resource(lookup = "java:/PostgresNC")
private DataSource dataSource;


@Override
public void init() throws ServletException {
    super.init();
    try {
        productDAOImpl = new ProductDAOImpl(dataSource);
    } catch (Exception exc) {
        throw new ServletException(exc);
    }
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        String theCommand = request.getParameter("command");

        if (theCommand == null) {
            theCommand = "PRODUCT_LIST";
        }

        switch (theCommand) {
            case "PRODUCT_LIST":
                listProducts(request, response);
                break;
            case "ADD_UPDATE":
                addOrUpdateProduct(request, response);
                break;
            default:
                listProducts(request, response);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void listProducts(HttpServletRequest request, HttpServletResponse response) throws Exception {

    List<Product> products = productDAOImpl.getAllProducts();

    request.setAttribute("PRODUCT_LIST", products);

    RequestDispatcher dispatcher = request.getRequestDispatcher("/list-products.jsp");
    dispatcher.forward(request, response);
}

private void addOrUpdateProduct(HttpServletRequest request, HttpServletResponse response) throws Exception {
    // read product from the form
    String idString = request.getParameter("productId");
    String category = request.getParameter("category");
    String description = request.getParameter("description");
    String productName = request.getParameter("productName");
    String price = request.getParameter("price");
    BigDecimal priceBigDecimal = new BigDecimal(price);
    String brand = request.getParameter("brand");

    try {
        int id = Integer.parseInt(idString.trim());
        int numCategory = Integer.parseInt(category);

        if(idString != null && category != null) {

            Product theProduct = new Product(id, numCategory, description, productName, priceBigDecimal, brand);

            // add the product to the database
            productDAOImpl.saveOrUpdate(theProduct);
        }
    }
    catch(NumberFormatException nfe) {
        nfe.printStackTrace();
    }

    listProducts(request, response);
}

ProductDAOImpl.java

public class ProductDAOImpl implements ProductDAO {
private JdbcTemplate jdbcTemplate;

//private DataSource dataSource;

public ProductDAOImpl(DataSource dataSource) {
    jdbcTemplate = new JdbcTemplate(dataSource);
}

@Override
public List<Product> getAllProducts() {
    String sql = "SELECT * FROM products ORDER BY prod_id\n" +
            "ASC";
    List<Product> productList = jdbcTemplate.query(sql, new RowMapper<Product>() {
        @Override
        public Product mapRow(ResultSet resultSet, int rowNum) throws SQLException {
            Product theProduct = new Product();

            theProduct.setProd_id(resultSet.getInt("prod_id"));
            theProduct.setCategory(resultSet.getInt("category"));
            theProduct.setDescription(resultSet.getString("description"));
            theProduct.setProductName(resultSet.getString("productName"));
            theProduct.setPrice(resultSet.getBigDecimal("price"));
            theProduct.setBrand("brand");

            return theProduct;
        }
    });
    return productList;
}

@Override
public void saveOrUpdate(Product newProduct) {
    if (newProduct.getProd_id() > 0) {
        // update
        String sql = "UPDATE products "
                + "SET category=?, description=?, prod_name=?, price=?, brand=? "
                + "WHERE prod_id=?";

        jdbcTemplate.update(sql, newProduct.getCategory(), newProduct.getDescription(),
                newProduct.getProductName(), newProduct.getPrice(), newProduct.getBrand());
    } else {
        // insert
        String sql = "INSERT INTO products (prod_id, category, description, prod_name, price, brand) "
                + "VALUES (?, ?, ?, ?, ?, ?)";

        jdbcTemplate.update(sql, newProduct.getProd_id(), newProduct.getCategory(), newProduct.getDescription(),
                newProduct.getProductName(), newProduct.getPrice(), newProduct.getBrand());
    }
} }
Ivan T
  • 35
  • 2
  • 10
  • 1
    The most possible reason for NoClassDefFoundError is 1) you have not package spring-jdbc jar file into your war file. 2) you have package it to the war file but your application server contains another spring-jdbc jar file that is of another version which is not compatible to your source code. – diufanman Dec 08 '16 at 03:50

1 Answers1

0

The required JAR file isn't available, so what you need to do is to place it on your CLASSPATH. You might choose to add the corresponding JAR file to your CLASSPATH, or as an alternate, you can download the JAR file from here.

A similar question on the same topic, for your reference is here and here.

Community
  • 1
  • 1
N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46