繁体   English   中英

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

[英]NoClassDefFoundError while running the spring application in command prompt

下面是我的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

将您的Spring库添加到类路径

命令行应该是

>java -classpath <all the libraries jars here> test.Client

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM