# Create a jar file for a JAX-WS (using Ant)

In 
Published 2022-12-03

This tutorial explains to you how to create a jar file for JAX-WS deployment using Ant.

In order to create the jar file for deployment here are some prerequisites:

  • You have installed the Apache Ant tool.
  • You have installed Java 8 (or 11) on your (test) machine
  • Your environment is Windows
  • In the %PATH% environment variable I have "C:\JAVA\ant\ant-1.10\bin;" where "C:\JAVA\ant\ant-1.10" = %ANT_HOME%.
  • %JAVA_HOME% is set up as well

When the source files (.java files) are created it is supposed to have these files in one ore more directories like this:

All these files are in my case in C:\JAVA\Eclipse-NEON2\workspace\JAX-WS-RPC\src\ws.

Now you have to create the build.xml file. Take a look at the following picture to see its location:

The main task is to write this XML file. Here is the build.xml file I used:

<project name="JAX-WS-RPC" default="main" basedir=".">
    <description>
        Create a JAX-WS using RPC Style JAR with Ant build script
    </description>
 
    <property name="projectName" value="JAX-WS-RPC">
 
    <!-- Java sources -->
    <property name="src.dir" location="src">
 
    <!-- Java classes -->
    <property name="build.dir" location="bin">
 
    <!-- Output, Jar -->
    <property name="dist.dir" location="dist">
 
    <target name="init">
        <!-- Create the time stamp -->
        <tstamp>
        <!-- Create the build directory structure used by compile -->
        <mkdir dir="${build.dir}">
    </mkdir></tstamp></target>
 
    <target name="compile" depends="init" description="compile the source ">
        <!-- Compile the java code from ${src.dir} into ${build.dir} -->
        <javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}">
    </javac></target>
 
    <target name="dist" depends="compile" description="package, output to JAR">
 
        <!-- Create the distribution directory -->
        <mkdir dir="${dist.dir}">
 
        <!-- Put everything in ${build} into the {$projectName}-${DSTAMP}.jar file -->
        <jar jarfile="${dist.dir}/${projectName}-${DSTAMP}.jar" basedir="${build.dir}">
            <manifest>
            <!-- create an executable Jar -->
            <attribute name="Main-Class" value="ws.HelloWorldServerPublisher">
            </attribute></manifest>
        </jar>
    </mkdir></target>
 
    <target name="clean" description="clean up">
        <delete dir="${build.dir}">
        <delete dir="${dist.dir}">
    </delete></delete></target>
 
    <!-- Default, run this -->
    <target name="main" depends="clean, compile, dist"></target>
    </property>
    </property>
    </property>
    </property>

</project>

Now you have just to run the following command:

ant dist

The jar file is created and after that can be run it: