在命令提示符下运行spring应用程序时出现NoClassDefFoundError

时间:2015-12-31 11:20:23

标签: java spring exception

下面是我的Spring代码。当我尝试编译程序时,所有类都已成功编译,并且在运行程序NoClassDefFoundError时出现。

MainMethodCLass:

package test;

import beans.SpringTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
public class Client
{
  public static void main(String [] raja)
  {
    //find xml
    Resource r = new ClassPathResource("resources/spring.xml");
    //load xml into container
    BeanFactory fact = new XmlBeanFactory(r);

    //create test class object
    Object o = fact.getBean("t");
    SpringTest t = (SpringTest)o;
    t.hello();
  }
}

简单的POJO课程:

package beans;
public class SpringTest
{
  public void hello()
  {
      System.out.println("Hello World");
  }
}

包资源中的spring.xml文件:

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
  "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
 <bean id = "t" class = "beans.Test">
 </bean>

</beans>

当我编译这个程序时,它已成功编译,但是当我编译时,引发了以下错误:

D:\Corejava>java test.Client
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/io/Resource
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.springframework.core.io.Resource
        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)
        ... 6 more

4 个答案:

答案 0 :(得分:0)

将弹簧库添加到类路径

命令行应该是

lastChangedDate

答案 1 :(得分:0)

您的路径中有多个,版本不同。删除一个,使用org.springframework.core下载依赖项。

答案 2 :(得分:0)

您需要在类路径和lib文件夹中包含jar spring-core-*.jar文件 在你的calss中,这个import org.springframework.core.*都在核心jar文件中。

答案 3 :(得分:0)

java.lang.NoClassDefFoundError - 此错误并不意味着JVM无法在类路径中找到您的类文件。要么它找到了类,但引用的类必须引用一些未加载的依赖类,或者有多个定义。使用Maven加载Spring lib。