In this post I explained the procedure to create executable jar file using ant.
You need to create java project using eclipse , create a lib folder in your eclipse project.
Here is example of a java class having main method.
You need to create java project using eclipse , create a lib folder in your eclipse project.
Here is example of a java class having main method.
package
com.test;
import
org.junit.Assert;
public
class VerifyString {
public boolean matchString(String str1, String str2){
if(str1.equals(str2)){
System.out.println("both strings are
equal");
return true;
}
else
{
System.out.println("both strings are
not equal");
return false;
}
}
public static void main(String[] str){
VerifyString
verifyString = new VerifyString();
Assert.assertTrue(verifyString.matchString("Akib",
"Akib"));
Assert.assertFalse(verifyString.matchString("Akib",
"Sachin"));
}
}
Put above java class in to your java project and put all required jar file into lib folder.
Build.xml
<?xml
version="1.0" encoding="iso-8859-1"?>
<project
name="Executable-Jar" default=""
basedir=".">
<!-- =======
Initialize Properties ================== -->
<property
environment="env"/>
<property
name="src" location="src/com"/>
<property
name="build" location="build"/>
<path
id="test.classpath">
<fileset
dir="lib" includes="*.jar"/>
</path>
<!-- clean
-->
<target
name="clean">
<delete
dir="${test.dest}"/>
</target>
<!-- compile -->
<target name="compile"
depends="clean" >
<delete
includeemptydirs="true" quiet="true">
<fileset dir="${test.dest}"
includes="**/*"/>
</delete>
<echo
message="making directory..."/>
<mkdir
dir="${build}"/>
<!-- <echo message="classpath:
${test.classpath}"/> -->
<echo message="compiling..."/>
<javac
debug="true"
destdir="${build}"
includes="*/*.java"
srcdir="${src}"
target="1.6"
classpathref="test.classpath"
></javac>
</target>
<!-- execute
jar fle -->
<target
name="run" depends = "Create_Jar">
<java
classname="com.test.VerifyString">
<classpath>
<pathelement
location="Junit-Jar.jar"/>
</classpath>
</java>
</target>
<!--
*********** create executable jar file ************ -->
<target name="Create_Jar"
depends="compile">
<jar
destfile="Junit-Jar.jar"
filesetmanifest="mergewithoutmain">
<manifest>
<attribute name="Main-Class"
value="com.test.VerifyString"/>
<attribute name="Class-Path"
value="."/>
</manifest>
<fileset dir="build"/>
<zipfileset
excludes="META-INF/*.SF" src="lib/junit-4.8.2.jar"/>
</jar>
</target>
</project>
Put above build.xml file into your project directory and run using command ant.
ant run
After executing above command, a “Junit-Jar.jar “ jar file should be created under project directory and executed main method.
Created jar file also can be executed using command
Java -jar Junit-Jar.jar
When I will execute as ant run its executed but folder build does not created and jar file
ReplyDeletealso not created. ow to resolve it