Using Ant to execute class file

This build.xml file is used to compile and run the java file and print the value on command prompt.

Using Ant to execute class file

Using Ant to execute class file

     

This build.xml file is used to compile and run the java file and print the value on command prompt. Here we are using five targets, the "clean" target deletes any previous "build", "classes" and "jar" directory; second one is used to create all necessary directories and it depends on <target name="clean">; third one is used to compile the java file from "src" directory to transform source files in to object files in the appropriate location in the build directory and it depends on <target name="prepare">; fourth one is used to create the jar file and it depends on <target name="compile">, it means that after completion of compile target, it will be executed; fifth one is used to run the jar file and it depends on <target name="jar"> and finally <target name="main"> is used to specify the default project name, it means that when the program will be run, it will be called first and it depends on <target name="run">. 

 

 

<?xml version="1.0"?>
<project name="Roseindia" default="main" basedir=".">
  <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="Roseindia"/>
  <target name="clean">
  <delete dir="${classes.dir}"/>
  <delete dir="${jar.dir}"/>
  <delete dir="${build.dir}"/>
  </target>
  <target name="prepare" depends="clean">
  <mkdir dir="${build.dir}"/>
  <mkdir dir="${classes.dir}"/>
  <mkdir dir="${jar.dir}"/>
  <mkdir dir="${src.dir}"/>
  </target>
  <target name="compile" depends="prepare">  
  <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
  </target>
  <target name="jar" depends="compile">  
 <jar destfile="${jar.dir}/${ant.project.name}.jar" 
 basedir=
"${classes.dir}">

  <manifest>
 <attribute name="Main-Class" value="${main-class}"/>
  </manifest>
  </jar>
  </target>
  <target name="run" depends="jar">
  <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
  </target>  
  <target name="main" depends="run">
  <echo message="main target completed.." />
  </target>
</project>

 If you run this build.xml file using ant command, then the following output will be displayed.

In this output display, the class name Roseindia is not found by the compiler. Create a java file as shown below and put it in the src directory, and again run it with ant command. The following output will be displayed.

class Roseindia{  
  public static void main(String args[]){
  System.out.println("Roseindia Technology Pvt. Ltd.");
  }
}


Download Source Code