似乎人们也有类似的问题,但在 IDE 上。我没有使用 IDE。
正如您将在我的 build.xml 文件中看到的那样,我使用 Maven 依赖项安装了 Spring。
我得到以下堆栈跟踪,显示 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
构建成功总时间:4 秒
这是我的客户端(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 文件,其中包含 spring 依赖项和类路径以及要使用 MAVEN *的 xmlns编辑:将类路径添加到 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>
最后,这是我的 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>
如果有人可以帮助我解释为什么即使 spring 依赖项在我的 build.xml 中(根据 build.xml 的输出成功下载)并且在类路径中也找不到该类,我将不胜感激。
提前致谢。