Monday, June 2, 2014

Creating a jar file through eclipse

Its rather simple and straight forward to create a jar file using eclipse. I am employing an ant build file to do this stuff.

 

Create a project and add a class to it as follows

package com.self.jarTest;

 

public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Hello World");

    }

}

 

Now create a build.xml file in the root of your project folder with the following content

<project>

 

    <target name="clean">

        <delete dir="build"/>

    </target>

 

    <target name="compile">

        <mkdir dir="build/classes"/>

        <javac srcdir="src" destdir="build/classes"/>

    </target>

 

    <target name="jar">

        <mkdir dir="build/jar"/>

        <jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">

            <manifest>

                <attribute name="Main-Class" value="com.self.jarTest.HelloWorld"/>

            </manifest>

        </jar>

    </target>

 

    <target name="run">

        <java jar="build/jar/HelloWorld.jar" fork="true"/>

    </target>

 

</project>

 

Now right click on the build.xml and click on “Run As -> 3.Ant Build -> Clean, Compile, run, jar -> Run“

 

That’s it you got your jar file J

 

No comments:

Post a Comment