java“hello world”的简单ant文件

时间:2012-09-17 02:19:23

标签: java ant

我正在尝试运行ant文件,但我一直收到以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError

我的build.xml文件包含:

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

如何解决此错误?

编辑:这是我的Proj0.java文件和堆栈跟踪的样子:

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

1 个答案:

答案 0 :(得分:5)

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

应该是

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

(类名,而不是源文件名)。