Neeraj Verma
Neeraj Verma

Reputation: 2334

Spring HelloWorld : Exception in thread "main" java.lang.NoClassDefFoundError

**Edit : I have copy pasted another **

I am simply trying to implement Spring HelloWorld example . My Spring version is 4.5 , jdk 5.1 I am trying to do it without maven , Just imported spring JAR in eclipse

  1. my main class(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. my 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.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();  
}  
}  

still it is saying "Exception in thread "main" java.lang.NoClassDefFoundError"


is it anything I am missing , do i need to integrate with maven . kindly suggest

this is my error stack

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

Upvotes: 2

Views: 1144

Answers (2)

DS_CodeGeek
DS_CodeGeek

Reputation: 11

This exception mainly happen when you are not imported commons-logging-1.1.3.jar. Go to Build Path > Configure Build Path > Libraries > Add External JARs. Select commons-logging-1.1.3.jar and apply.

Upvotes: 0

Anil Agrawal
Anil Agrawal

Reputation: 3026

I can see you have copied some code from a tutorial.

But you didn't copied correctly.

You have defined your bean in spring.xml like below,

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

But there is no class with name testClass with variable name

I think, now you can do the rest of things

Upvotes: 2

Related Questions