• 社区居民
  • 忠实会员
  • 原创写手

SPRING java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext

更多 发布于:2016-05-12 14:42
It seems people have had similar problems, but on IDE's. I am not using an IDE. I installed Spring using a Maven dependency as you will see in my build.xml file. I get the following stacktrace that says org.springframework.context is not found: run-decouple: [java] java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
 [java]     at java.lang.Class.getDeclaredMethods0(Native Method)
[java]     at java.lang.Class.privateGetDeclaredMethods(
 [java]     at java.lang.Class.getMethod0(
 [java]     at java.lang.Class.getMethod(
 [java]     at sun.launcher.LauncherHelper.getMainMethod(
 [java]     at sun.launcher.LauncherHelper.checkAndLoadMain(
 [java] Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
 [java]     at$
 [java]     at$
 [java]     at Method)
 [java]     at
 [java]     at java.lang.ClassLoader.loadClass(
 [java]     at sun.misc.Launcher$AppClassLoader.loadClass(
  [java]     at java.lang.ClassLoader.loadClass(
 [java]     ... 6 more
 [java] Exception in thread "main"
 [java] Java Result: 1
 BUILD SUCCESSFUL Total time: 4 seconds Here is my client (java file: package com.example.decouple.client;

import java.util.*;
import com.example.filereader.IReader;
import org.springframework.context.ApplicationContext;

public class DecoupledDataReaderClient {
  private IReader reader = null;
  private ApplicationContext ctx = null;
  public DecoupledDataReaderClient() {
    ctx = new ClassPathXmlApplicationContext("beans.xml");
  private String fetchData() {
    reader = (IReader) ctx.getBean("reader");
  public static void main(String[] args) {
    DecoupledDataReaderClient client = new DecoupledDataReaderClient();
    System.out.println("Example 1.3: Got data: " + client.fetchData());
 Here is my build.xml file with the spring dependency & classpath and xmlns to use MAVEN *EDIT: ADDED THE CLASSPATH TO RUN-DECOUPLE* <project name="data-reader" default="compile" xmlns:artifact="antlib:org.apache.maven.artifact.ant">
    <property name="src" location="."/>
    <property name="obj" location="../classes"/>
    <property name="jarfile" location="../data-reader.

    <target name="init">
        <mkdir dir="dist" />
        <artifact:dependencies pathId="dependency.classpath">
            <dependency groupId="org.springframework"
                        version="3.2.4.RELEASE" />

    <target name="compile" depends="init">
        <mkdir dir="${obj}"/>
        <depend srcdir="${src}" destdir="${obj}" />
        <javac includeantruntime="false"
            <classpath refid="dependency.classpath" />

    <target name="jar" depends="compile">
        <jar basedir="${obj}" destfile="${jarfile}">
            <include name="*.class"/>

    <target name="run" depends="compile">
        <java classpath="${obj}" classname="VanillaDataReaderClient"/>

    <target name="decouple-jar" depends="compile">
        <jar destfile="dist/decouple.jar" basedir="${obj}">
                <attribute name="Main-Class" value="com.example.decouple.client.DecoupledDataReaderClient"/>

    <target name="run-decouple" depends="decouple-jar">
    <java fork="true" jar="dist/decouple.jar" >
            <path refid="dependency.classpath" />

 And lastly, here is my beans.xml file <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

    <bean name="reader" class="com.example.filereader.VanillaFileReader">
        <constructor-arg value="src/hello.txt" />
 If someone could help me with why it is not finding the class even though the spring dependency is in my build.xml (which downloaded successfully according to the output of build.xml) and with it in the classpath, I would greatly appreciate it. Thanks in advance.artifact:dependencies is storing the classpath for the Spring dependencies in the path dependency.classpath, and you're using that when you compile: <javac ...
    <classpath refid="dependency.classpath" />
 However, when you run, you're only including your compiled source in the classpath: <target name="run" depends="compile">
    <java classpath="${obj}" classname="VanillaDataReaderClient"/>
 This means the Spring classes aren't included. Try including dependency.classpath as well: <target name="run" depends="compile">
    <java classname="VanillaDataReaderClient">
            <pathelement location="${obj}"/>
            <path refid="dependency.classpath"/>
 Edit: Your run-decouple target is using jar: "When using the jar attribute, all classpath settings are ignored". Switch to specifying the main class and the classpath, so you can include the dependencies: <target name="run-decouple" depends="decouple-jar">
    <java fork="true" classname="com.example.decouple.client.DecoupledDataReaderClient">
            <pathelement location="dist/decouple.jar" />
            <path refid="dependency.classpath" />










喜欢0 评分0