Spring HelloWorld:线程中的异常" main" java.lang.NoClassDefFoundError

时间:2017-04-20 15:00:29

标签: java spring javabeans

**编辑:我复制粘贴另一个**

我只是想实现Spring HelloWorld示例。 我的Spring版本是4.5,jdk 5.1 我试图在没有maven的情况下进行,只需在eclipse中导入spring JAR

  1. 我的主要课程(Student.java)

    package com.javatpoint;  
    
    public class Student {  
    private String name;  
    
    public String getName() {  
        return name;  
    }  
    
    public void setName(String name) {  
        this.name = name;  
    }  
    
    public void displayInfo(){  
        System.out.println("Hello: "+name);  
    }  
    }
    
  2. 我的spring.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"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-
    3.0.xsd">  
    
     <bean id="studentbean" class="com.javatpoint.Student">  
     <property name="name" value="Vimal Jaiswal"></property>  
    

  3. 3.Test.java

             package com.javatpoint;  
    
    import org.springframework.beans.factory.BeanFactory;  
    import org.springframework.beans.factory.xml.XmlBeanFactory;  
    import org.springframework.core.io.ClassPathResource;  
    import org.springframework.core.io.Resource;  
    
    import com.javatpoint.Student;
    
    public class Test {  
    public static void main(String[] args) {  
        Resource resource=new ClassPathResource("applicationContext.xml");  
        BeanFactory factory=new XmlBeanFactory(resource);  
    
        Student student=(Student)factory.getBean("studentbean");  
        student.displayInfo();  
    }  
    }  
    

    仍然是在&#34;线程中的例外&#34;主要&#34; java.lang.NoClassDefFoundError&#34;

    是我缺少的东西,我需要与maven集成吗?请建议

    这是我的错误堆栈

    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.<init>(DefaultSingletonBeanRegistry.java:83)
        at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.<init>(FactoryBeanRegistrySupport.java:43)
        at org.springframework.beans.factory.support.AbstractBeanFactory.<init>(AbstractBeanFactory.java:175)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.<init>(AbstractAutowireCapableBeanFactory.java:160)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.<init>(AbstractAutowireCapableBeanFactory.java:171)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.<init>(DefaultListableBeanFactory.java:193)
        at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:78)
        at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:67)
        at com.javatpoint.Test.main(Test.java:13)
    Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
        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)
        ... 9 more
    

2 个答案:

答案 0 :(得分:2)

我可以看到你复制了教程中的一些代码。

但你没有正确复制。

您已在spring.xml中定义了bean,如下所示

<bean id="helloBean" class="testClass">
    <property name="name" value="Mkyong" />
</bean>

但是没有名称 testClass 的类,其中变量名称

我想,现在你可以做其他事情了

答案 1 :(得分:0)

此异常主要发生在未导入commons-logging-1.1.3.jar时。转到构建路径&gt;配置构建路径&gt;图书馆&gt;添加外部JAR。选择commons-logging-1.1.3.jar并申请。