Sukhdeep Singh Handa
Sukhdeep Singh Handa

Reputation: 413

NoClassDefFoundError : org/springframework/context/support/ClassPathXmlApplicationContext

I am using spring-framework-3.2.0.RELEASE to make a simple application. But i get NoClassDefFoundError

Here is my code

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.FileNotFoundException;

public class SpringDemo{
    public static void main(String args[]) throws FileNotFoundException{
            ApplicationContext context = new ClassPathXmlApplicationContext("message.xml");
    }
}

class MessageClass{
    private String message = null;

    MessageClass(String message){
        this.message = message;
    }

    public String getMessage(){
        return message;
    }

    public void setMessage(String message){
        this.message = message;
    }
}

and following is my message.xml file

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

<bean id="message"
      class="MessageClass">
    <property name="message" value="Spring is fun." />
</bean>

I get following error

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/c
ontext/support/ClassPathXmlApplicationContext
        at SpringDemo.main(SpringDemo.java:10)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.support
.ClassPathXmlApplicationContext
        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)
        ... 1 more

I have included following jars in my jdk_version/jre/lib/ext directory

1) antlr 2) commons-logging-1.1.1 3) spring-beans 4) spring-context 5) spring-core 6) spring-jdbc 7) spring-orm 8) spring-tx

What is wrong? I didn't get any help from other related questions. Please help me out.

Thanks in advance.

Upvotes: 0

Views: 3875

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122394

I have included following jars in my jdk_version/jre/lib/ext directory

This is probably a mistake, as only JARs that need to be extensions should go in this directory (fif you're not 100% sure that a particular JAR needs to be an extension, then it probably doesn't). Try removing the JARs from lib/ext and putting them on your application's classpath in the normal way instead.

Upvotes: 2

Related Questions