Java 使用Ant编译和运行时出现NoClassDefFoundError
我正试图使用Ant编译和运行一个项目,结果发现一个NoClassDefFoundError。这是我的OutlookToGmailCalendarSync.java文件,位于src文件夹中:Java 使用Ant编译和运行时出现NoClassDefFoundError,java,ant,noclassdeffounderror,build.xml,Java,Ant,Noclassdeffounderror,Build.xml,我正试图使用Ant编译和运行一个项目,结果发现一个NoClassDefFoundError。这是我的OutlookToGmailCalendarSync.java文件,位于src文件夹中: package sample.calendar; public class OutlookToGmailCalendarSync { public static void main(String[] args) { System.out.println("hi"); } }
package sample.calendar;
public class OutlookToGmailCalendarSync {
public static void main(String[] args) {
System.out.println("hi");
}
}
这是我的build.xml文件:
<project name="MyCalendarSample" default="run" basedir=".">
<description>
simple example build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<path id="path.class">
<pathelement location="build/sample/calendar"/>
</path>
<target name="run" depends="compile"
description="Runs the complied project">
<!-- Run -->
<java fork="true" classname="OutlookToGmailCalendarSync">
<classpath>
<path refid="path.class"/>
</classpath>
</java>
</target>
<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} directory trees -->
<delete dir="${build}"/>
</target>
</project>
即使在阅读了其他论坛的帖子后,我仍然不明白为什么这会发生在我身上。在运行时,编译的类位于build/sample/calendar中,这是类路径设置的位置,因此我看不出问题。尝试以下更改: 类路径设置为生成类的根目录,并为java命令提供类的打包名称
<path id="path.class">
<pathelement location="build"/>
</path>
<target name="run" depends="compile"
description="Runs the complied project">
<!-- Run -->
<java fork="true" classname="sample.calendar.OutlookToGmailCalendarSync" >
<classpath refid="path.class"/>
</java>
</target>
希望这会有所帮助
<path id="path.class">
<pathelement location="build"/>
</path>
<target name="run" depends="compile"
description="Runs the complied project">
<!-- Run -->
<java fork="true" classname="sample.calendar.OutlookToGmailCalendarSync" >
<classpath refid="path.class"/>
</java>
</target>