使用Ant编译和运行时NoClassDefFoundError

时间:2013-03-31 02:04:16

标签: 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");
    }
}

这是我的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>

这就是结果:

C:\java\my>ant
Buildfile: C:\java\my\build.xml

init:

compile:
    [javac] C:\java\my\build.xml:30: warning: 'includeantruntime' was not set, d
efaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 1 source file to C:\java\my\build

run:
     [java] java.lang.NoClassDefFoundError: OutlookToGmailCalendarSync (wrong na
me: sample/calendar/OutlookToGmailCalendarSync)
     [java]     at java.lang.ClassLoader.defineClass1(Native Method)
     [java]     at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
     [java]     at java.security.SecureClassLoader.defineClass(SecureClassLoader
.java:142)
     [java]     at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
     [java]     at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
     [java]     at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
     [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]     at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.j
ava:482)
     [java] Exception in thread "main"
     [java] Java Result: 1

BUILD SUCCESSFUL
Total time: 1 second

C:\java\my>

即使在阅读其他论坛帖子后,我仍然不明白为什么会发生这种情况。在运行时,编译的类在build / sample / calendar中,这是类路径设置的内容,所以我没有看到问题。

1 个答案:

答案 0 :(得分:2)

尝试以下更改:

Classpath设置为生成类的根目录,并为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>

希望这会有所帮助。