Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.ClassNotFoundException: org.springframework.expression.ParserContext Spring HelloWorld?

Tags:

java

spring

maven

I have simple Spring HelloWorld program. Its contents are as follows:

enter image description here

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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mahesha999.examples.spring</groupId>
    <artifactId>SpringExamples</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>       
    </dependencies>

</project>

eg1.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

    <bean id="helloBean" class="com.mahesha999.examples.spring.eg1.HelloWorld">
        <property name="name" value="Mahesha999" />
    </bean>

</beans>

HelloWorld.java

public class HelloWorld {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public void printHello() {
        System.out.println("Spring  : Hello ! " + name);
    }
}

App.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("eg1.xml");

        HelloWorld obj = (HelloWorld) context.getBean("helloBean");
        obj.printHello();
    }
}

This gave me following error:

INFO: Loading XML bean definitions from class path resource [eg1.xml]
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/expression/ParserContext
    at org.springframework.context.support.AbstractApplicationContext.prepareBeanFactory(AbstractApplicationContext.java:628)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:516)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.mahesha999.examples.spring.eg1.App.main(App.java:8)
Caused by: java.lang.ClassNotFoundException: org.springframework.expression.ParserContext
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 5 more

When I changed spring version from 4.3.2 to 4.2.2 in pom.xml, it worked fine. Why is it so?

like image 676
Mahesha999 Avatar asked Aug 24 '16 11:08

Mahesha999


1 Answers

I was also getting the same error and it got fixed after adding the spring-expression jar

like image 148
Programming-Lover Avatar answered Oct 10 '22 09:10

Programming-Lover