【问题标题】:java.lang.ClassNotFoundException: org.springframework.expression.ParserContext Spring HelloWorld?java.lang.ClassNotFoundException:org.springframework.expression.ParserContext Spring HelloWorld?
【发布时间】:2016-08-24 11:37:35
【问题描述】:

我有一个简单的 Spring HelloWorld 程序。其内容如下:

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();
    }
}

这给了我以下错误:

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

当我在 pom.xml 中将 spring 版本从 4.3.2 更改为 4.2.2 时,它运行良好。为什么会这样?

【问题讨论】:

标签: java spring maven


【解决方案1】:

我也遇到了同样的错误,添加 spring-expression jar 后它得到了修复

【讨论】:

    【解决方案2】:

    我在 Eclipse 中测试了您的代码,它在 4.2.2.RELEASE 和 4.3.2.RELEASE 中都能正常工作。它在消息下方打印。您能否清理您的构建或从命令提示符运行构建。

    Spring  : Hello ! Mahesha999
    

    【讨论】:

    • 关闭“自动构建”,改为4.3.2,在命令提示符下从项目目录尝试maven install,在eclipse中刷新项目并运行App类。同样的错误。用相同的步骤改回4.2.2。东西开始工作了
    • 您可以尝试从本地 .m2 存储库中删除文件,或者您可以在另一台机器上构建和运行您的项目。正如我所说,这个项目对我来说两个版本都很好。
    • 从本地 .m2 存储库中删除所有文件并重建应用程序为我解决了这个问题。谢谢!
    【解决方案3】:

    只有在没有“spring-expression”库时才会出现此错误。
    解决方案1: 如果添加 spring-expression 库后仍然存在,请尝试添加新版本的 spring 库。

    解决方案 2: 删除您的 spring.xml 文件及其 packg,然后使用 spring.xml 创建新包。

    【讨论】:

      【解决方案4】:
      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
      

      【讨论】:

        【解决方案5】:

        解决方案:在您的项目中添加“spring-expression-4.3.11.RELEASE.jar”并重新构建您的项目。

        【讨论】:

          猜你喜欢
          • 2021-08-15
          • 2018-12-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多