Context initialization failed Spring

48,880

Solution 1

It looks like you may be trying to use Java 8 with Spring 3.2.0, which doesn't support it. You will need to use Spring 4 instead, or downgrade to Java 7.

Solution 2

Use SPRING 3.2.3.RELEASE. This will fix this error caused by JDk 1.8.

You have to update this in POM.xml

Share:
48,880
Isma9
Author by

Isma9

Updated on July 21, 2022

Comments

  • Isma9
    Isma9 almost 2 years

    I am making a sample project following the steps of a website and is giving me a weird bug and I can not fix it, and I'm desperate :(.

    enter image description here

    HelloController.java:

    package com.companyname.springapp.web;
    
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    
        public class HelloController {
    
            protected final Log logger = LogFactory.getLog(getClass());
    
            @RequestMapping(value="/hello.htm")
            public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
                    throws ServletException, IOException {
    
                logger.info("Returning hello view");
    
                return new ModelAndView("hello.jsp");
            }
        }
    

    HelloControllerTest.java

    package com.companyname.springapp.web;
    
    import static org.junit.Assert.*;
    
        import org.junit.Test;
        import org.springframework.web.servlet.ModelAndView;
    
    
        public class HelloControllerTest {
    
            @Test
            public void testHandleRequestView() throws Exception{       
                HelloController controller = new HelloController();
                ModelAndView modelAndView = controller.handleRequest(null, null);       
                assertEquals("hello.jsp", modelAndView.getViewName());
            }
    
        }
    

    app-config.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <!-- Scans the classpath of this application for @Components to deploy as 
            beans -->
        <context:component-scan base-package="com.companyname.springapp.web" />
    
        <!-- Configures the @Controller programming model -->
        <mvc:annotation-driven />
    
    </beans>
    

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
        <display-name>Springapp</display-name>
        <servlet>
            <servlet-name>springapp</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/spring/app-config.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>springapp</servlet-name>
            <url-pattern>*.htm</url-pattern>
        </servlet-mapping>
    
    </web-app>
    

    hello.jsp

    <html>
      <head><title>Hello :: Spring Application</title></head>
      <body>
        <h1>Hello - Spring Application</h1>
        <p>Greetings.</p>
      </body>
    </html>
    

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.companyname</groupId>
      <artifactId>springapp</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>springapp Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
      </dependencies>
      <build>
        <finalName>springapp</finalName>
      </build>
      <properties>
        <org.springframework.version>3.2.0.RELEASE</org.springframework.version>
      </properties>
    </project>
    

    Error:

    SEVERE: Context initialization failed
    java.lang.IllegalArgumentException
        at org.springframework.asm.ClassReader.<init>(Unknown Source)
        at org.springframework.asm.ClassReader.<init>(Unknown Source)
        at org.springframework.asm.ClassReader.<init>(Unknown Source)
        at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:52)
        at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80)
        at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:101)
        at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:76)
        at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:298)
        at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:300)
        at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:300)
        at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:230)
        at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:153)
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:130)
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:285)
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:223)
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:630)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
        at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:647)
        at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:598)
        at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:661)
        at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:517)
        at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:458)
        at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138)
        at javax.servlet.GenericServlet.init(GenericServlet.java:158)
        at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
        at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
        at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5210)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5493)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    
    jul 22, 2014 12:06:12 PM org.apache.catalina.core.ApplicationContext log
    SEVERE: StandardWrapper.Throwable
    java.lang.IllegalArgumentException
        at org.springframework.asm.ClassReader.<init>(Unknown Source)
        at org.springframework.asm.ClassReader.<init>(Unknown Source)
        at org.springframework.asm.ClassReader.<init>(Unknown Source)
        at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:52)
        at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80)
        at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:101)
        at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:76)
        at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:298)
        at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:300)
        at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:300)
        at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:230)
        at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:153)
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:130)
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:285)
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:223)
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:630)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
        at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:647)
        at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:598)
        at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:661)
        at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:517)
        at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:458)
        at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138)
        at javax.servlet.GenericServlet.init(GenericServlet.java:158)
        at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
        at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
        at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5210)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5493)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    
    jul 22, 2014 12:06:12 PM org.apache.catalina.core.StandardContext loadOnStartup
    SEVERE: Servlet /springapp threw load() exception
    java.lang.IllegalArgumentException
        at org.springframework.asm.ClassReader.<init>(Unknown Source)
        at org.springframework.asm.ClassReader.<init>(Unknown Source)
        at org.springframework.asm.ClassReader.<init>(Unknown Source)
        at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:52)
        at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80)
        at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:101)
        at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:76)
        at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:298)
        at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:300)
        at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:300)
        at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:230)
        at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:153)
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:130)
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:285)
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:223)
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:630)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
        at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:647)
        at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:598)
        at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:661)
        at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:517)
        at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:458)
        at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138)
        at javax.servlet.GenericServlet.init(GenericServlet.java:158)
        at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
        at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
        at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5210)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5493)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    

    And the web where I am following the steps is:

    http://www.uv.es/grimo/teaching/SpringMVCv3PasoAPaso/part1.html

    thank you very much.

  • Isma9
    Isma9 almost 10 years
    Okay! thank you very much, but I'm new to this, could tell me how it's done?
  • Dave Morrissey
    Dave Morrissey almost 10 years
    It may be as simple as changing your spring version to 4.0.6.RELEASE but there may be changes that aren't compatible with the tutorial you're following. I don't know as I haven't tried it yet! Downgrading java is relatively simple, and you can google for solutions to that, which will depend on your OS.
  • Isma9
    Isma9 almost 10 years
    I changed the version of Spring in the pom.xml and still gives me the same error. I put this: <org.springframework.version>4.0.6.RELEASE</org.springframew‌​ork.version>
  • Dave Morrissey
    Dave Morrissey almost 10 years
    Make sure you've removed all the spring 3 libraries from your project (depends how you're building it but look in web-inf/lib). Run mvn clean as well.
  • eclipz905
    eclipz905 about 9 years
    I had to change the JRE version used by my server (Tomcat). If you are managing the server from Eclipse, it will be set to use the default JRE for the workspace. You can change the JRE for just the server by going to "Window > Preferences > Server > Runtime Environment", then editing your server preferences to use a Java 7 JRE. Alternatively, you can change the JRE for the entire workspace at "Window > Preferences > Java > Compiler"