9

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?

Mahesha999
  • 22,693
  • 29
  • 116
  • 189

5 Answers5

13

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

Programming-Lover
  • 1,177
  • 10
  • 14
4

I tested you code in eclipse and it is working fine with both 4.2.2.RELEASE and 4.3.2.RELEASE. It is printing below message. Can you please clean you build or run build from command prompt.

Spring  : Hello ! Mahesha999
abaghel
  • 14,783
  • 2
  • 50
  • 66
  • turned off "Build automatically", changed to `4.3.2`, tried `maven install` from project directory in command prompt, refreshed the project in eclipse and ran the App class. Same error. Changed back to `4.2.2` with same steps. Stuff started working – Mahesha999 Aug 24 '16 at 14:29
  • 2
    You can try deleting files from local .m2 repositories or you can build and run your project on another machine. As I said this project is working fine for me for both the versions. – abaghel Aug 25 '16 at 04:04
  • Deleting all the files from the local .m2 repository and rebuilding the app solved this issue for me. Thanks! – Abhi Sep 18 '16 at 16:10
0

You will get this error only when there is no "spring- expression" lib.
solution 1: In case after adding spring-expression lib still exists then try adding new version of spring library.

solution 2: Delete your spring.xml file along with its packg and then create new package with spring.xml.

Ragav
  • 1
  • 2
0
i have use spring 4.3.11 version

so add the bellow jar into lib folder

1. commons-logging-1.1.1.jar
2. servlet-api.jar
3. spring-aop-4.3.11.RELEASE.jar
4. spring-beans-4.3.11.RELEASE.jar
5. spring-context-4.3.11.RELEASE.jar
6.spring-core-4.3.11.RELEASE.jar
7. spring-expression-4.3.11.RELEASE.jar
8. spring-web-4.3.11.RELEASE.jar
9.spring-webmvc-4.3.11.RELEASE.jar
Avinash Khadsan
  • 441
  • 3
  • 6
0

Solution : Add the "spring-expression-4.3.11.RELEASE.jar" in your project and the rebuild your project.

Ravan
  • 1