2

I'm trying to run ant file and I keep getting the following error:

Exception in thread "main" java.lang.NoClassDefFoundError

My build.xml file contains this:

<project name="Proj0" default="compile" basedir=".">

  <description>
    Proj0 build file
  </description>

  <!-- global properties for this build file -->
  <property name="source.dir" location="src"/>
  <property name="build.dir" location="bin"/>
  <property name="doc.dir" location="doc"/>
  <property name="main.class" value="proj0.Proj0.java"/>

  <!-- set up some directories used by this project -->
  <target name="init" description="setup project directories">
    <mkdir dir="${build.dir}"/>
    <mkdir dir="${doc.dir}"/>
  </target>

  <!-- Compile the java code in ${src.dir} into ${build.dir} -->
  <target name="compile" depends="init" description="compile java sources">
    <javac srcdir="${source.dir}" destdir="${build.dir}"/>
  </target>

  <!-- execute the program with the fully qualified name in ${build.dir} -->
  <target name="run" description="run the project">
    <java dir="${build.dir}" classname="${main.class}" fork="yes">
        <arg line="${args}"/>
    </java>
  </target>

  <!-- Delete the build & doc directories and Emacs backup (*~) files -->
  <target name="clean" description="tidy up the workspace">
    <delete dir="${build.dir}"/>
    <delete dir="${doc.dir}"/>
    <delete>
      <fileset defaultexcludes="no" dir="${source.dir}" includes="**/*~"/>
    </delete>
  </target>

  <!-- Generate javadocs for current project into ${doc.dir} -->
  <target name="doc" depends="init" description="generate documentation">
    <javadoc sourcepath="${source.dir}" destdir="${doc.dir}"/>
  </target>

</project>

How do I resolve this error?

EDIT: This is what my Proj0.java file and stack trace look like:

package proj0;

public class Proj0 
{
    public static void main(String[] args)
    {
        System.out.println("Hello World");
    }
}

Buildfile: build.xml

run:
     [java] Exception in thread "main" java.lang.NoClassDefFoundError: proj0/Proj0
     [java] Caused by: java.lang.ClassNotFoundException: proj0.Proj0
     [java]     at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
     [java]     at java.security.AccessController.doPrivileged(Native Method)
     [java]     at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
     [java]     at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
     [java]     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
     [java]     at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
     [java] Could not find the main class: proj0.Proj0. Program will exit.
     [java] Java Result: 1

BUILD SUCCESSFUL
Total time: 0 seconds
LTH
  • 1,779
  • 3
  • 19
  • 21

1 Answers1

5
  <property name="main.class" value="proj0.Proj0.java"/>

That should probably be

 <property name="main.class" value="proj0.Proj0"/>

(the class name, not the source file name).

Thilo
  • 257,207
  • 101
  • 511
  • 656