Create Jar Ant Build Hello World

Create Jar Ant Build Hello World

In this demo you will see how to create and run jar file using ant build file. We will print Hello World on the console to test it.

  • Sample project structure:

Create Jar Ant Build Hello World

  • HelloBean.java class:
package com.javahonk;


public class HelloBean {
	
	public String sayHello() {
		return "Hello World!!!!!";
	}
}
  • HelloWorld.java:
package com.javahonk;

public class HelloWorld {

	public static void main(String[] args) {
		
		HelloBean helloBean = new HelloBean();
		System.out.println(helloBean.sayHello());

	}

}
  • build.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Jboss 5.0 Ear Deployment with Servlet and JSP" default="run" basedir=".">

	<property name="lib.home" value="${basedir}/lib" />

	<!-- Define the CLASSPATH -->
	<path id="compile.classpath">
		<fileset dir="${basedir}/lib">
			<include name="*.jar"/>
		</fileset>
	</path>

	<target name="clean">
		<delete dir="build"/>
	</target>

	<target name="compile">
		<mkdir dir="build/classes"/>
		<javac srcdir="src" destdir="build/classes" includeantruntime="false">
			<classpath refid="compile.classpath"/>
		</javac>
	</target>

	<target name="jar" depends="clean,compile">
		<mkdir dir="build/jar"/>
		<jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
			<manifest>
				<attribute name="Main-Class" value="com.javahonk.HelloWorld"/>
			</manifest>
			<include name="**/ejbModule/META-INF/*.xml" />
		</jar>
	</target>

	<target name="run" depends="clean,compile,jar">
		<java jar="build/jar/HelloWorld.jar" fork="true"/>
	</target>

</project>
  • To run build to create and run jar file: Right click build.xml –> Run As –> Ant Build you will see below output with Hello World printed on the console:

Create Jar Ant Build Hello World

 

  • To run jar from command line please use below command:

Create Jar Ant Build Hello World

download Download Project: HelloWorldJar

For more information about ant build please refer Apache ant here

Leave a Reply

Your email address will not be published. Required fields are marked *