More Tutorials| Bioinformatics| Open Source| Photoshop| Questions?
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Join and Excel yourself with our Online instructor led training sessions
Training Courses
Tell A Friend
Your Friend Name

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.