Apache Ant – Build Java Application With External Libraries


Development environment

  1. JDK 1.8 Downloads
  2. Eclipse Mars Download

Required jar files


In order to run the example; simply follow following steps:

Step 1 Create Java Project

  • Launch Eclipse IDE
  • Going to File -> New -> Others… -> Java Project.
  • Choose any project name you want then click Finish.

Step 2 Create Java Class

Copy the code below to clipboard; Select src folder in your project; Press CTRL + V; Eclipse IDE will automatically create package & class file with the code that’s pasted from clipboard.

Step 3 Create log4j.properties

Create an empty file named log4j.properties under src folder; and paste the content below:

Step 4 Configure Build Path…

  1. Download Apache log4j 1.2.17; and then to unpack it.
  2. Create lib folder by right click to the project select New -> Folder. Copy the log4j-1.2.17.jar file from the unpacked folder; and paste to the lib folder.
  3. Right click to the project; select Build Path -> Configure Build Path… Select Libraries tab; click Add Jars… Select the jar file then click Ok.  Click Ok to finish the configuration.

Step 5 Configure Ant

Step 5.1 Create build.xml

  • Right click to the project; select New -> Others… 
  • Select XML File under XML category
  • Click Next.
  • Type build.xml in File Name field.
  • Click Finish.

Step 5.2 Configure Runtime Environment

  • Right click to HelloWorld in Ant window; select Run As -> External Tools Configurations
  • Select JRE tab
  • Click Installed JREs… button to add/remove or edit JRE definition.
  • Click Add button.
  • Select Standard VM; then click Next.
  • Click Directory button to select the root folder of JDK installation. Select the folder then click Ok.
  • Click Finish.
  • Check the JDK then click Ok.
  • CLick Apply.
  • Click Close.

Step 5.2 Run Ant

  • From menu bar; select Windows -> Show Views ->  Others…
  • Select Ant under Ant category.
  • Right click to Ant window; select Add buildfiles…
  • Select the build.xml file then click Ok.

You can see 3 targets in build.xml are:

  • clean to delete the generated folder and everything inside.
  • compile to create folder named build, compile code and copy all file in src folder except .java files; and copy all files in lib folder to build\classes\lib folder.
  • jar to create jar folder in build folder, generate jar file from files and folders in build\classes folder that’s generated by compile target.

Double click to jar target; Ant will compile and generate files and folder as defined in build.xml and generate the jar file.

Out put in console window

Build Folder Structure

Download Source Code here