[英]Spring: Keep getting java.lang.ClassNotFoundException: org.springframework.expression.ParserContext
[英]java.lang.ClassNotFoundException: org.springframework.expression.ParserContext Spring HelloWorld?
我有簡單的 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>
你好世界
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public void printHello() {
System.out.println("Spring : Hello ! " + name);
}
}
應用程序.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 時,它工作正常。 為什么會這樣?
我也遇到了同樣的錯誤,在添加 spring-expression jar 后它得到了修復
我在 eclipse 中測試了你的代碼,它在 4.2.2.RELEASE 和 4.3.2.RELEASE 中都可以正常工作。 它正在打印以下消息。 您能否從命令提示符清理您的構建或運行構建。
Spring : Hello ! Mahesha999
只有當沒有“spring-expression”庫時,您才會收到此錯誤。
解決方案 1:如果添加 spring-expression 庫后仍然存在,請嘗試添加新版本的 spring 庫。
解決方案 2:刪除 spring.xml 文件及其 packg,然后使用 spring.xml 創建新包。
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
解決方案:在您的項目中添加“spring-expression-4.3.11.RELEASE.jar”並重建您的項目。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.