Haven’t worked with Apache Ant for a long time. It’s time to pick up it again. Here is a simple Java project could be built by Ant.
Project hierarchy
- hello-world-ant (project root folder)
- src
- com
- eureka
- HelloWorld.java
- build.xml
HelloWorld.java
package com.eureka;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
build.xml
<project name="hello-world-ant" basedir="." default="main">
<!-- Ant properties -->
<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="com.eureka.HelloWorld"/>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<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="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>
</project>
Done =)
Reference: Tutorial: Hello World with Apache Ant


Hi…followed the above steps :
am getting an error in the run target
Error: Could not find or load main class com.eureka.HelloWorld
how do i solve the error?
LikeLike
Can you check the MANIFEST.MF inside the built hello-world-ant.jar. it should contains the following text.
LikeLike
how do i get the hello-world-ant.jar in the readable format?
LikeLike
Open it with 7-zip
LikeLike
verified the MANIFEST.MF,contains the following:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.7.0_21-b11 (Oracle Corporation)
Main-Class: com.eureka.HelloWorld
LikeLike
no idea on the error. could u zip your project and send to my email?
yingkityuen(at)gmail(dot)com
LikeLike