Announcement Announcement Module
Collapse
No announcement yet.
Exception in thread CannotLoadBeanClassException java.lang.ClassNotFoundException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Exception in thread CannotLoadBeanClassException java.lang.ClassNotFoundException

    I am new to spring framework, trying to run a very basic application,getting following exception

    Exception in thread "main" org.springframework.beans.factory.CannotLoadBeanCl assException: Cannot find class [Car] for bean with name 'Car' defined in class path resource [Travel.xml]; nested exception is java.lang.ClassNotFoundException: Car
    at org.springframework.beans.factory.support.Abstract BeanFactory.resolveBeanClass(AbstractBeanFactory.j ava:1138)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.predictBeanType(Abstrac tAutowireCapableBeanFactory.java:522)
    at org.springframework.beans.factory.support.Abstract BeanFactory.isFactoryBean(AbstractBeanFactory.java :1174)
    at org.springframework.beans.factory.support.Abstract BeanFactory.isFactoryBean(AbstractBeanFactory.java :754)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:422)
    at org.springframework.context.support.AbstractApplic ationContext.finishBeanFactoryInitialization(Abstr actApplicationContext.java:729)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:381)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:139)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:83)
    at basic_spring.Travel.main(Travel.java:13)
    Caused by: java.lang.ClassNotFoundException: Car
    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)
    at org.springframework.util.ClassUtils.forName(ClassU tils.java:249)
    at org.springframework.beans.factory.support.Abstract BeanDefinition.resolveBeanClass(AbstractBeanDefini tion.java:381)
    at org.springframework.beans.factory.support.Abstract BeanFactory.resolveBeanClass(AbstractBeanFactory.j ava:1135)
    ... 9 more

    My XML file looks like this

    <?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-2.0.xsd">

    <bean id="Car" class="Car">
    <property name="Wheel" ref="Wheel"/>
    </bean>
    <bean id="Wheel" class="BridgeStone"/>
    </beans>


    My classes look as follows

    Wheel.java
    package basic_spring;

    public interface Wheel {
    public void rotate();
    }

    BridgeStone.java
    public class BridgeStone implements Wheel{
    public void rotate(){
    System.out.println("Wheel Rotates");
    }
    }

    Car.java
    package basic_spring;

    public class Car {
    public Wheel wheel;
    public Wheel getWheel(){
    return wheel;
    }
    public void setWheel(Wheel wheel){
    this.wheel=wheel;
    }
    public void move(){
    System.out.println("Car is moving");
    this.getWheel().rotate();
    }
    }

    Travel.java
    package basic_spring;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlAp plicationContext;

    public class Travel {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    ApplicationContext ctx= new ClassPathXmlApplicationContext("Travel.xml");
    Car car = (Car)ctx.getBean("Car");
    car.move();
    }

    }

    Please suggest any possible solution for this simple issue. Thanks in Advance.

  • #2
    Got Solution

    Thanks for looking into my issue.. I got solution for a my silly issue

    just small modification to Travel.xml (just needed to add package name to class declaration)

    <bean id="Car" class="basic_spring.Car">
    <property name="wheel" ref="Wheel"/>
    </bean>
    <bean id="Wheel" class="basic_spring.BridgeStone"/>



    Originally posted by nbarot View Post
    I am new to spring framework, trying to run a very basic application,getting following exception

    Exception in thread "main" org.springframework.beans.factory.CannotLoadBeanCl assException: Cannot find class [Car] for bean with name 'Car' defined in class path resource [Travel.xml]; nested exception is java.lang.ClassNotFoundException: Car
    at org.springframework.beans.factory.support.Abstract BeanFactory.resolveBeanClass(AbstractBeanFactory.j ava:1138)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.predictBeanType(Abstrac tAutowireCapableBeanFactory.java:522)
    at org.springframework.beans.factory.support.Abstract BeanFactory.isFactoryBean(AbstractBeanFactory.java :1174)
    at org.springframework.beans.factory.support.Abstract BeanFactory.isFactoryBean(AbstractBeanFactory.java :754)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:422)
    at org.springframework.context.support.AbstractApplic ationContext.finishBeanFactoryInitialization(Abstr actApplicationContext.java:729)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:381)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:139)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:83)
    at basic_spring.Travel.main(Travel.java:13)
    Caused by: java.lang.ClassNotFoundException: Car
    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)
    at org.springframework.util.ClassUtils.forName(ClassU tils.java:249)
    at org.springframework.beans.factory.support.Abstract BeanDefinition.resolveBeanClass(AbstractBeanDefini tion.java:381)
    at org.springframework.beans.factory.support.Abstract BeanFactory.resolveBeanClass(AbstractBeanFactory.j ava:1135)
    ... 9 more

    My XML file looks like this

    <?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-2.0.xsd">

    <bean id="Car" class="Car">
    <property name="Wheel" ref="Wheel"/>
    </bean>
    <bean id="Wheel" class="BridgeStone"/>
    </beans>


    My classes look as follows

    Wheel.java
    package basic_spring;

    public interface Wheel {
    public void rotate();
    }

    BridgeStone.java
    public class BridgeStone implements Wheel{
    public void rotate(){
    System.out.println("Wheel Rotates");
    }
    }

    Car.java
    package basic_spring;

    public class Car {
    public Wheel wheel;
    public Wheel getWheel(){
    return wheel;
    }
    public void setWheel(Wheel wheel){
    this.wheel=wheel;
    }
    public void move(){
    System.out.println("Car is moving");
    this.getWheel().rotate();
    }
    }

    Travel.java
    package basic_spring;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlAp plicationContext;

    public class Travel {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    ApplicationContext ctx= new ClassPathXmlApplicationContext("Travel.xml");
    Car car = (Car)ctx.getBean("Car");
    car.move();
    }

    }

    Please suggest any possible solution for this simple issue. Thanks in Advance.

    Comment

    Working...
    X