嵌套异常是 java.lang.NoClassDefFoundError:无法初始化类 org.springframework.jdbc.support.SQLErrorCodesFactory
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21327497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.jdbc.support.SQLErrorCodesFactory
提问by arpho
I am writing an application with spring and jdbctemplate. However I am encountering the following error:
我正在用 spring 和 jdbctemplate 编写一个应用程序。但是我遇到以下错误:
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:
这是我的 web.xml:
<?xml version="1.0" encoding="UTF-8"?>
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
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:
<?xml version="1.0" encoding="UTF-8"?>
http://www.springframework.org/schema/beans/spring-beans.xsd">
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?
可能有什么问题?
采纳答案by Ashish
Required jar is missing SO you have to download the required jar and then set your classpath download link
缺少所需的 jar,因此您必须下载所需的 jar,然后设置类路径 下载链接
回答by Maheshkumar
回答by Keerthivasan
When NoClassDefFoundError
is thrown, Directly from the documentation
什么时候NoClassDefFoundError
抛出,直接从文档
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.
如果 Java 虚拟机或 ClassLoader 实例尝试加载类的定义(作为正常方法调用的一部分或作为使用 new 表达式创建新实例的一部分)并且找不到类的定义,则抛出。编译当前正在执行的类时,搜索到的类定义存在,但无法再找到该定义。
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
在你的情况下,它是这个 Class org.springframework.jdbc.support.SQLErrorCodesFactory
。因此,我们需要在类加载期间通过提供包含此类的 .jar 文件将此类提供给类加载器或 JVM。您正在使用 Spring-jdbc API。因此,您需要将此 jar 添加org.springframework.jdbc-X.X.X.RELEASE
到类路径中。这个 jar 文件可以从链接下载。我已经给出了3.0.5版本的下载链接。请将其更改为所需的版本。如果您使用 Maven 作为构建工具,则可以POM
像这样将其作为依赖项添加到项目文件中
<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.
希望您理解清楚以解决此错误。