Question

I'm trying to compile and run a project using Ant and I'm getting a NoClassDefFoundError. This is my OutlookToGmailCalendarSync.java file located in the src folder:

package sample.calendar;
public class OutlookToGmailCalendarSync {
    public static void main(String[] args) {
        System.out.println("hi");
    }
}

This is my build.xml file:

<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>

And this is the result:

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>

Even after reading other forum posts, I still don't understand why this is happening to me. At runtime, the compiled class is in build/sample/calendar, which is what the classpath is set to, so I don't see the problem.

Était-ce utile?

La solution

Try with the below change:

The Classpath is set to the root directory where the classes are generated and class's packaged name is provided for java command.

<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>

Hope this would help.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top