Jay SenSharma

Jay SenSharma

Here is a Simple Program to demonstrate that How we can Compile a Java Program Using ANT, How we can Make a Jar of it as well as How we can run this Java Program by Just pressing ENTER Key from your Keynoard.

Step 1). Create “src” directory and Develop a Java Program Like “HelloWorld.java” inside the “src” directory.

package hello;
public class HelloWorld
public static void main(String ar[])
System.out.println(“nnt Hello Mr. “+ar[0]);
System.out.println(“nnt Country “+ar[1]);
Step2). Provide the ANT Build file
<project name=”RunJava” default=”run” 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=”jar.file.name” value=”HelloWorld”/>
<target name=”init” depends=”clean”>
<mkdir dir=”${build.dir}”/>
<target name=”clean”>
<delete dir=”build”/>
<target name=”compile” depends=”init”>
<mkdir dir=”${classes.dir}”/>
<javac srcdir=”${src.dir}” destdir=”${classes.dir}” />
<target name=”jar” depends=”compile”>
<mkdir dir=”${jar.dir}”/>
<jar destfile=”${jar.dir}/${jar.file.name}.jar” basedir=”${classes.dir}”/>
<target name=”run” depends=”jar”>
<java classname=”hello.HelloWorld” classpath=”${jar.dir}/${jar.file.name}.jar” >
<arg line=”Jack INDIA” />
Step 3). Now Run the ANT ..better open a command prompt and then run “setDomainEnv.cmd” or “setWLSEnv.cmd” from WebLogic Installation directory. and then run ANT
Buildfile: jarBuild.xml
[delete] Deleting directory F:DELETEANT_Testbuild
[mkdir] Created dir: F:DELETEANT_Testbuild
[mkdir] Created dir: F:DELETEANT_Testbuildclasses
[javac] Compiling 1 source file to F:DELETEANT_Testbuildclasses
[mkdir] Created dir: F:DELETEANT_Testbuildjar
[jar] Building jar: F:DELETEANT_TestbuildjarHelloWorld.jar

Hello Mr. Jack</div>

Country INDIA

Total time: 10 seconds


Now As soon as we start running our ANT script first of all the ANT reads the first line
<project name=”RunJava” default=”run” basedir=”.”> and searches for the default Target name…As we haven’t specified any ant target while Running ANT script…That’s why It has Picked up the Default ANT Target “run”.

Now As soon as ANT tool Execution Pointer Reaches to
<target name=”run” depends=”jar”>
the above target. It searches for Any other dependency of this target by looking at “depends” attribute of our current Target. As the “run” Target has a Dependency on “jar” target depends=”jar”. The ant execution Pointer reaches to the <target name=”jar” depends=”compile”> to execute it. But again the “jar” target has dependency on “compile” target and similarly “compile” Target has a dependency on “init” …and “init” target has a Dependency on “clean” Target …thats why we see that …. Execution Sequence of this ant is

clean —> init —> compile —> jar —> run

Jay SenSharma
If you enjoyed this post, please considerleaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.