4
votes

I am writing an application with spring and jdbctemplate. However I am encountering the following error:

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.jdbc.support.SQLErrorCodesFactory
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:972)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause:

java.lang.NoClassDefFoundError: Could not initialize class org.springframework.jdbc.support.SQLErrorCodesFactory
org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.setDataSource(SQLErrorCodeSQLExceptionTranslator.java:89)
org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.<init>(SQLErrorCodeSQLExceptionTranslator.java:78)
org.springframework.jdbc.support.JdbcAccessor.getExceptionTranslator(JdbcAccessor.java:58)
org.springframework.jdbc.support.JdbcAccessor.afterPropertiesSet(JdbcAccessor.java:71)
org.springframework.jdbc.core.JdbcTemplate.<init>(JdbcTemplate.java:115)
com.metmi.mmasgis.dao.DbImpl.getDatabases(DbImpl.java:29)
com.metmi.mmasgis.HomeController.dbs(HomeController.java:61)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

this is my web.xml:

<?xml version="1.0" encoding="UTF-8"?>

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

my root-context.xml:

<?xml version="1.0" encoding="UTF-8"?>

http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- Root Context: defines shared resources visible to all other web components -->


<bean id="dataSource" name="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="username" value="root"></property>
    <property name="password" value="abc"></property>
    <property name="url"
        value="jdbc::mysql://localhost:3306/mmasgis">
    </property>
</bean>

What could be wrong?

3
if my answer is helpful you can select my answerAshish

3 Answers

3
votes

Required jar is missing SO you have to download the required jar and then set your classpath download link

4
votes

When NoClassDefFoundError is thrown, Directly from the documentation

Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found. The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.

In your case, it is this Class org.springframework.jdbc.support.SQLErrorCodesFactory. So, we need to provide this class to the classloader or JVM during class loading by providing the .jar file containing this class. You are using Spring-jdbc API. so, you need to add this jar org.springframework.jdbc-X.X.X.RELEASE to the classpath. This jar file can be downloaded from link. I have given the download link of 3.0.5 version. Please change it to the required version. If you are using Maven as build tool, you can add it as a dependency to the POM file of your project like this

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>org.springframework.jdbc</artifactId>
  <version>3.0.5.RELEASE</version>
</dependency>

Hope you understand it clearly to resolve this error.

0
votes

Spring JDBC jar file missing from your classpath (mostly under WEB-INF/lib).

Refer this link for clear understanding.