Easy learning with example program codes

how to build java project using ant in eclipse?

As we discussed all basic concepts of Apache ant, let us see the below example to build java project using ant.

In build.xml file:

1. src.dir: It specify the project source folder.
2. build.dir: It specify the project compilation output folder.

Example explanation:

When we run the build.xml file, control first go on the project element and look for the default target. In our example mainTarget is the default target so control go on the mainTarget. Then control looks for those targets on which mainTarget depends. The mainTarget depends on compile target which is further depends upon clean and makedir target. So first clean target will execute followed by makedir, compile and mainTarget. The javac task in the compile target is responsible for compiling the code.


 * This program is used to find that given number is Armstrong or not.
 * @author codesjava
public class ArmstrongNumber {
	 * This method is used to find that 
         * given number is Armstrong or not.
	 * @param num
	static void armstrong(int num){
		int newNum = 0, reminder, temp;
		temp = num;
		//find sum of all digit's cube of the number.
		while(temp != 0){
			reminder = temp % 10;
			newNum = newNum + reminder*reminder*reminder;
			temp = temp/10;
		//Check if sum of all digit's cube of the number is 
		//equal to the given number or not.
		if(newNum == num){
		  System.out.println("Given no. is an armstrong no.");
		  System.out.println("Given no. is not an armstrong no.");
	public static void main(String args[]){
		//method call


<?xml version="1.0" encoding="UTF-8"?>
 <!-- Set the project name, basedir and default target to be executed-->
<project name="Ant-Build-Project-Test" default="mainTarget" basedir=".">
  <!-- Sets the properties here-->
  <property name="src.dir" location="src" />
  <property name="build.dir" location="bin" />
  <!-- Target for deleting the existing directories-->
  <target name="clean">
    <delete dir="${build.dir}" />
  <!-- Target for creating the new directories-->
  <target name="makedir">
    <mkdir dir="${build.dir}" />
  <!-- Target for compiling the java code-->
  <target name="compile" depends="clean, makedir">
    <javac srcdir="${src.dir}" destdir="${build.dir}">
  <!-- Defualt target to run all targets-->
  <target name="mainTarget" depends="compile">
    <description>Main target</description>


Buildfile: D:\TestWorkspace\AntBuildProject\build.xml
   [delete] Deleting directory D:\TestWorkspace\AntBuildProject\bin
    [mkdir] Created dir: D:\TestWorkspace\AntBuildProject\bin
    [javac] D:\TestWorkspace\AntBuildProject\build.xml:20: 
    warning: 'includeantruntime' was not set, defaulting to 
    build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 1 source file to 
Total time: 688 milliseconds

Download this example.
Next Topic: How to create java document using ant in eclipse with example?
Previous Topic: Ant build file.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.


Sign Up/ Sign In
Ask a Question

Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at


We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on

Copyright © 2018 CodesJava Protection Status SiteMap Reference: Java Wiki