Classdefnotfound exception while using bean.xml in

2019-08-10 16:26发布

问题:

org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@fb509a: startup date [Fri Jul 17 21:34:24 IST 2015]; root of context hierarchy Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/OrderComparator$OrderSourceProvider at org.springframework.context.support.AbstractRefreshableApplicationContext.createBeanFactory(AbstractRefreshableApplicationContext.java:200) at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:126) at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:452) at org.springframework.context.support.FileSystemXmlApplicationContext.(FileSystemXmlApplicationContext.java:140) at org.springframework.context.support.FileSystemXmlApplicationContext.(FileSystemXmlApplicationContext.java:84) at mySimpleSpringApp.myApp.main(myApp.java:14) Caused by: java.lang.ClassNotFoundException: org.springframework.core.OrderComparator$OrderSourceProvider 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) ... 7 more

my main class ::

 package mySimpleSpringApp;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;

    public class myApp {
      public static void main(String[] args) {
            ApplicationContext appContext = new FileSystemXmlApplicationContext("appContext.xml");

            Fruit f = appContext.getBean("fruit", Fruit.class);
            Vegetable v = (Vegetable)appContext.getBean("vegetable");

            System.out.println(f.talkAboutYourself());
            System.out.println(v.talkAboutYourself());

        }

    }

bean xml file :: appContext.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.xsd">

<bean id="fruit" class="mySimpleSpringApp.Fruit"></bean>
<bean id="vegetable" class="mySimpleSpringApp.Vegetable" />

</beans>

What i am doing wrong here?

This question might be duplicate but i did not get answer from other post, as those solutions did not work for me.

回答1:

The NoClassDefFoundError is thrown by JVM at runtime when it try to load a class that is not present in the classpath.

Check if the class in present in the classpath or not.

May be a jar is not added in the right position or it is not correctly referenced in the classpath, or the jar version is not the right one.

Note the OrderSourceProvider is present since spring 4.1. Check if the jar loaded at runtime is older than that version.