我是一个学习创建ant构建文件的学生,我在/ src中有一些.java源文件,在/ lib中有一个.jar库(json.jar)。我已经在build.xml中的javac的类路径中包含了.jar,它编译得很好,但是在执行时失败了:
[echo] command line execute: java -cp classes:lib/json.jar Main
[java] Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONString
[java] at java.base/java.lang.ClassLoader.defineClass1(Native Method)
[java] at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1007)
[java] at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
[java] at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:801)
[java] at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:699)
[java] at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:622)
[java] at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:580)
[java] at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
[java] at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
[java] at Main.main(Unknown Source)
[java] Caused by: java.lang.ClassNotFoundException: org.json.JSONString
[java] at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
[java] at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
[java] at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
[java] ... 10 more
[java] Java Result: 1
这是我的build.xml:
<?xml version="1.0"?>
<project name="Ant-Test" default="targets" basedir=".">
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="src.dir" location="src" />
<property name="build" location="classes" />
<property environment="env"/>
<property name="lib" value="lib" />
<property name="user" value="${env.USERNAME}"/>
<path id="compile.classpath">
<pathelement location="${build}"/>
<pathelement path="lib/json.jar"/>
</path>
<target name="targets">
<echo message="Targets are clean, prepare, build, execute, and targets"/>
</target>
<path id="compile.classpath">
<pathelement location="${build}"/>
</path>
<!-- Deletes the existing build, docs and dist directory-->
<target name="clean">
<delete dir="${build}" />
</target>
<!-- Creates the build, docs and dist directory-->
<target name="prepare">
<mkdir dir="${build}" />
</target>
<!-- Compiles the java code (including the usage of library for JUnit -->
<target name="build" depends="prepare">
<javac srcdir="${src.dir}" includeantruntime="false" destdir="${build}" classpath="lib/json.jar">
<classpath refid="compile.classpath"/>
</javac>
</target>
<target name="execute" depends="build" description="Run the program">
<echo message="command line execute: java -cp classes:lib/json.jar Main" />
<java classname="Main" fork="yes">
<classpath refid="compile.classpath"/>
</java>
</target>
</project>
我不确定还有什么可以尝试,感谢任何帮助!