问题描述
似乎人们有类似的问题,但在IDE上。
我没有使用IDE。
It seems people have had similar problems, but on IDE's.I am not using an IDE.
我使用Maven依赖关系安装了Spring,您将在我的build.xml文件中看到。
I installed Spring using a Maven dependency as you will see in my build.xml file.
我得到以下堆栈跟踪,表示找不到org.springframework.context:
[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$1.run(URLClassLoader.java:366)
[java] at java.net.URLClassLoader$1.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
这是我的客户端(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());
}
}
这是我的build.xml文件春天依赖& classpath和xmlns以使用MAVEN
* 编辑:将CLASSPATH添加到运行中 - *
<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>
最后,这里是我的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>
如果有人可以帮助我,为什么它没有找到课程,即使春天的依赖是在我的build.xml(根据build.xml的输出成功下载),并且在类路径中,我将非常感谢。
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.
提前感谢。 p>
Thanks in advance.
推荐答案
工件:依赖关系
正在将路径中的Spring依赖项存储在路径中 dependency.classpath
,而您在编译时正在使用:
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"/>
</target>
这意味着不包括Spring类。
This means the Spring classes aren't included.
尝试包括 dependency.classpath
以及
<target name="run" depends="compile">
<java classname="VanillaDataReaderClient">
<classpath>
<pathelement location="${obj}"/>
<path refid="dependency.classpath"/>
</classpath>
</java>
</target>
编辑:
您的运行解耦
目标使用 jar
:。切换到指定主类和类路径,因此可以包括依赖关系:
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">
<classpath>
<pathelement location="dist/decouple.jar" />
<path refid="dependency.classpath" />
</classpath>
</java>
</target>
这篇关于SPRING java.lang.NoClassDefFoundError:org / springframework / context / ApplicationContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!