Spring/Java org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [C:launchCodeFilessrcmainjavaRunMario.java]

Tags: , ,



I am learning Java and told to learn Spring. I am writing a simple program and get this error message (I am using IntelliJ Idea IDE):

first line of main
2015-05-06 11:37:38 INFO  ClassPathXmlApplicationContext:510 -   Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1e717c2: startup date [Wed May 06 11:37:38 CDT 2015]; root of context hierarchy
2015-05-06 11:37:38 INFO  XmlBeanDefinitionReader:317 - Loading XML bean definitions from class path resource [ApplicationContext.xml]
2015-05-06 11:37:39 WARN  ClassPathXmlApplicationContext:487 - Exception encountered during context initialization - cancelling refresh attempt
Exception in thread "main" org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [C:launchCodeFilessrcmainjavaRunMario.java] for bean with name 'obstacle1' defined in class path resource [ApplicationContext.xml]; nested exception is java.lang.ClassNotFoundException: C:launchCodeFilessrcmainjavaRunMario.java
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [C:launchCodeFilessrcmainjavaRunMario.java] for bean with name 'obstacle1' defined in class path resource [ApplicationContext.xml]; nested exception is java.lang.ClassNotFoundException: C:launchCodeFilessrcmainjavaRunMario.java
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1328)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1328)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:622)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:622)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:591)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:591)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1397)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1397)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:968)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:968)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:735)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:735)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at RunMario.main(RunMario.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at RunMario.main(RunMario.java:17)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
Caused by: java.lang.ClassNotFoundException: C:launchCodeFilessrcmainjavaRunMario.java
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.reflect.Method.invoke(Method.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
Caused by: java.lang.ClassNotFoundException: C:launchCodeFilessrcmainjavaRunMario.java
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:249)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:395)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1320)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:249)
... 15 more
at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:395)
at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1349)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1320)
... 15 more

Process finished with exit code 1

My ApplicationContext.xml file is as follows.

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

    <bean id="obstacle1" class="C:launchCodeFilessrcmainjavaRunMario.java">
    <constructor-arg name= "marioObstacles" ref="obstacle"/>
    </bean>

    <bean id="obstacle" class="C:launchCodeFilessrcmainjavaMarioObstacles.java">
    <constructor-arg name="obstacle" value="0"/>
    </bean>

</beans>

I copied and pasted the RunMario class file path from the project panel so it should be the fully qualified path. Here is the RunMario.java file:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Scanner;

public class RunMario {

    private MarioObstacles marioObstacles;

    public RunMario(MarioObstacles marioObstacles) {
    this.marioObstacles = marioObstacles;
    }
    public static void main(String[] arguments) {
   //        RunMario runMario1 = new    RunMario(MarioObstacles.getInstance());
        System.out.println("first line of main");

        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        System.out.println("just out of context");

        RunMario obj = (RunMario) context.getBean("obstacle1");
        obj.start();
    }

    public void start() {
        System.out.println("first line of start");
        BuildOstacles pyramid = marioObstacles.pyramid();
        pyramid.setHeight(runMario());
        pyramid.buildPyramid();
        System.out.println(pyramid);
        System.out.println("exiting start");
    }

    public static int runMario() {
        System.out.println("entering runMario");
        int height;
        do {
            Scanner scan = new Scanner(System.in);
            System.out.println("Please enter a whole number between 1 and 10");
            height = scan.nextInt();
        } while (height < 1 || height > 10);
        return height;
    }
}

I have spent 2 days searching for the answer. I cannot seem to figure it out.

Any suggestions?

Answer

You are right that you need fully-qualified class names, but in this context that means a package location, like com.yourapp.RunMario, not a file path.

Based on your code, you don’t have any packages defined (that is, you have no package at the top of your RunMario class). It is best practice to define packages, but until you do, your fully-qualified class is simply RunMario.

In other words, where you have

<bean id="obstacle1" class="C:launchCodeFilessrcmainjavaRunMario.java">
    <constructor-arg name= "marioObstacles" ref="obstacle"/>
</bean>

<bean id="obstacle" class="C:launchCodeFilessrcmainjavaMarioObstacles.java">
    <constructor-arg name="obstacle" value="0"/>
</bean>

it should be

<bean id="obstacle1" class="RunMario">
    <constructor-arg name= "marioObstacles" ref="obstacle"/>
</bean>

<bean id="obstacle" class="MarioObstacles">
    <constructor-arg name="obstacle" value="0"/>
</bean>


Source: stackoverflow