Spring - Loading XML bean definitions from a class path resource [Beans.xml] - NoClassDefFoundError

I'm new to Spring, and I was referring to this tutorial for my first Spring project. After completing the entire instruction, I try to run the program, but I have this error.

May 26, 2015 11:42:45 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@31221be2: startup date [Tue May 26 11:42:45 CST 2015]; root of context hierarchy
May 26, 2015 11:42:45 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Beans.xml]
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/expression/ParserContext
    at org.springframework.context.support.AbstractApplicationContext.prepareBeanFactory(AbstractApplicationContext.java:553)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:455)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.tutorialspoint.MainApp.main(MainApp.java:9)
Caused by: java.lang.ClassNotFoundException: org.springframework.expression.ParserContext
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 5 more

Any idea where I did wrong. As far as I know, I followed the textbook correctly.

THE CODE

package com.tutorialspoint;

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

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

      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");

      obj.getMessage();
   }
}

Beans.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
       <property name="message" value="Hello World!"/>
   </bean>

</beans>
+4
source share
4 answers

Check if you have added all the necessary banks to your class path.

The class org.springframework.expression.ParserContextis inside. spring-expresssion-YOUR_SPRING_VERSION.jarCheck if this bank is added to the class path.

+10
source

I had the same problem. But I found my mistake.

: "Beans.xml" com.tutorialspont.

"Beans.xml" src.

0

log4j: WARN (org.springframework.core.env.StandardEnvironment). log4j: WARN , log4j. "main" java.lang.NoClassDefFoundError: org/springframework/expression/PropertyAccessor    org.springframework.context.support.AbstractApplicationContext.prepareBeanFactory (AbstractApplicationContext.java:553)   at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:454)    org.springframework.context.support.ClassPamlApplicationContext. (ClassPamlApplicationContext.java:139)    org.springframework.context.support.ClassPamlApplicationContext. (ClassPamlApplicationContext.java:83)    com.javahonk.client.TestWebService.main(TestWebService.java:19) : java.lang.ClassNotFoundException: org.springframework.expression.PropertyAccessor    java.net.URLClassLoader $1.run(URLClassLoader.java:372)    java.net.URLClassLoader $1.run(URLClassLoader.java:361)    java.security.AccessController.doPrivileged( )    java.net.URLClassLoader.findClass(URLClassLoader.java:360)    java.lang.ClassLoader.loadClass(ClassLoader.java:424)   at sun.misc.Launcher $AppClassLoader.loadClass(Launcher.java:308)    java.lang.ClassLoader.loadClass(ClassLoader.java:357)   ... 5

Solution to the above problem: This happens if spring -expression-xxx.Release.jar is inaccessible in your class path. Add the spring -expression-3.2.3.RELEASE or latest fix to your class path to fix this problem.

-1
source

Actually, your class name is MainApp and in the .xml file that you mentioned as this class = "com.tutorialspoint.HelloWorld". But it should be like class = "com.tutorialspoint.MainApp". Please follow this action and then run, I think it will work correctly.

-1
source

All Articles