弹簧 java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19128424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:19:57  来源:igfitidea点击:

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

javaspringmavendependenciesnoclassdeffounderror

提问by jdelgado08

It seems people have had similar problems, but on IDE's. I am not using an IDE.

似乎人们有类似的问题,但在 IDE 上。我没有使用 IDE。

I installed Spring using a Maven dependency as you will see in my build.xml file.

我使用 Maven 依赖项安装了 Spring,您将在我的 build.xml 文件中看到。

I get the following stacktrace that says org.springframework.context is not found:

我得到以下堆栈跟踪,表明找不到 org.springframework.context:

run-decouple:

[java] java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
 [java]     at java.lang.Class.getDeclaredMethods0(Native Method)
[java]     at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
 [java]     at java.lang.Class.getMethod0(Class.java:2685)
 [java]     at java.lang.Class.getMethod(Class.java:1620)
 [java]     at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:488)
 [java]     at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:480)
 [java] Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
 [java]     at java.net.URLClassLoader.run(URLClassLoader.java:366)
 [java]     at java.net.URLClassLoader.run(URLClassLoader.java:355)
 [java]     at java.security.AccessController.doPrivileged(Native Method)
 [java]     at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
 [java]     at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
 [java]     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)

运行解耦:

[java] java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
 [java]     at java.lang.Class.getDeclaredMethods0(Native Method)
[java]     at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
 [java]     at java.lang.Class.getMethod0(Class.java:2685)
 [java]     at java.lang.Class.getMethod(Class.java:1620)
 [java]     at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:488)
 [java]     at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:480)
 [java] Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
 [java]     at java.net.URLClassLoader.run(URLClassLoader.java:366)
 [java]     at java.net.URLClassLoader.run(URLClassLoader.java:355)
 [java]     at java.security.AccessController.doPrivileged(Native Method)
 [java]     at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
 [java]     at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
 [java]     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
 [java]     at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
 [java]     ... 6 more
 [java] Exception in thread "main"
 [java] Java Result: 1

BUILD SUCCESSFUL Total time: 4 seconds

构建成功 总时间:4 秒

Here is my client (java file: DecoupledDataReaderClient.java):

这是我的客户端(java 文件:DecoupledDataReaderClient.java):

package com.example.decouple.client;

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

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");
    return reader.read();
  }
  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*

这是我的 build.xml 文件,其中包含 spring 依赖项和类路径以及 xmlns 以使用 MAVEN*编辑:将类路径添加到 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.
        jar"/>

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

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

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

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

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

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


</project>

And lastly, here is my beans.xml file

最后,这是我的 beans.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" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean name="reader" class="com.example.filereader.VanillaFileReader">
        <constructor-arg value="src/hello.txt" />
    </bean>
</beans>

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.

如果有人可以帮助我解决为什么即使 spring 依赖项在我的 build.xml(根据 build.xml 的输出已成功下载)并且在类路径中也找不到类的原因,我将不胜感激。

Thanks in advance.

提前致谢。

采纳答案by Joe

artifact:dependenciesis storing the classpath for the Spring dependencies in the path dependency.classpath, and you're using that when you compile:

artifact:dependencies将 Spring 依赖项的类路径存储在 path 中dependency.classpath,并且您在编译时使用它:

<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"/>
</target>

This means the Spring classes aren't included.

这意味着不包括 Spring 类。

Try including dependency.classpathas well:

尝试包括dependency.classpath

<target name="run" depends="compile">
    <java classname="VanillaDataReaderClient">
        <classpath>
            <pathelement location="${obj}"/>
            <path refid="dependency.classpath"/>
        </classpath>
    </java>
</target>

Edit: Your run-decoupletarget 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:

编辑:您的run-decouple目标正在使用jar:“使用 jar 属性时,将忽略所有类路径设置”。切换到指定主类和类路径,以便您可以包含依赖项:

<target name="run-decouple" depends="decouple-jar">
    <java fork="true" classname="com.example.decouple.client.DecoupledDataReaderClient">
        <classpath>
            <pathelement location="dist/decouple.jar" />
            <path refid="dependency.classpath" />
        </classpath>
    </java>
</target>