Using Ant to build a Java program/application

Howto :

  1. Create a Directory where you will build and will also contain java src file
  2. Create all source files (extension = .java)
  3. Example here uses Hello.java (Simple Hello World program which also dumps command line arguments)
// Hello.java
 
import java.io.*;
 
public class Hello
{
   public static void main (String[] argv)
   {
      System.out.print("Hello world");
          if (argv.length > 0)
          {
                System.out.println("Argument[0] = "+argv[0]);
          }
   }
}
<project default="compile">
  <target name="compile">
    <javac srcdir="." />
  </target>
 
  <target name="jar" depends="compile">
    <jar destfile="Hello.jar"
         basedir="."
         includes="**/*.class"
         />
  </target>
 
  <target name="run" depends="jar">
    <java classname="Hello"
          classpath="Hello.jar"
          fork="true"
          />
  </target>
</project>

Steps

  1. ant compile
  2. ant run

Back to Java

antbuildhowto.txt · Last modified: 2014/10/26 01:52 (external edit)
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0