Apache Ant – A simple Ant build project

Haven’t worked with Apache Ant for a long time. It’s time to pick up it again. Here is a simple Java project could be built by Ant.

Project hierarchy

- hello-world-ant (project root folder)
  - src
    - com
      - eureka
        - HelloWorld.java
  - build.xml



package com.eureka;

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



<project name="hello-world-ant" basedir="." default="main">

  <!-- Ant properties -->
  <property name="src.dir"     value="src"/>
  <property name="build.dir"   value="build"/>
  <property name="classes.dir" value="${build.dir}/classes"/>
  <property name="jar.dir"     value="${build.dir}/jar"/>
  <property name="main-class"  value="com.eureka.HelloWorld"/>

  <target name="clean">
    <delete dir="${build.dir}"/>

  <target name="compile">
    <mkdir dir="${classes.dir}"/>
    <javac srcdir="${src.dir}" destdir="${classes.dir}"/>

  <target name="jar" depends="compile">
    <mkdir dir="${jar.dir}"/>
    <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
        <attribute name="Main-Class" value="${main-class}"/>

  <target name="run" depends="jar">
    <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>

  <target name="clean-build" depends="clean,jar"/>

  <target name="main" depends="clean,run"/>



4. Run Ant!

Done =)

Reference: Tutorial: Hello World with Apache Ant


8 thoughts on “Apache Ant – A simple Ant build project”

  1. Hi…followed the above steps :
    am getting an error in the run target
    Error: Could not find or load main class com.eureka.HelloWorld
    how do i solve the error?


    1. Can you check the MANIFEST.MF inside the built hello-world-ant.jar. it should contains the following text.

      Manifest-Version: 1.0
      Ant-Version: Apache Ant 1.9.2
      Created-By: 1.7.0_25-b17 (Oracle Corporation)
      Main-Class: com.eureka.HelloWorld


  2. verified the MANIFEST.MF,contains the following:
    Manifest-Version: 1.0
    Ant-Version: Apache Ant 1.8.3
    Created-By: 1.7.0_21-b11 (Oracle Corporation)
    Main-Class: com.eureka.HelloWorld


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.