How Do I Automatically Generate A .jar File In An Eclipse Java Project

eclipse, eclipse-3.4, jar, java

Solution

Create an Ant file and tell Eclipse to build it. There are only two steps and each is easy with the step-by-step instructions below.

Step 1 Create a build.xml file and add to package explorer:

<?xml version="1.0" ?>
<!-- Configuration of the Ant build system to generate a Jar file --> 
<project name="TestMain" default="CreateJar">
  <target name="CreateJar" description="Create Jar file">
        <jar jarfile="Test.jar" basedir="." includes="*.class" />
  </target>
</project>

Eclipse should looks something like the screenshot below. Note the Ant icon on build.xml.

Step 2 Right-click on the root node in the project. - Select Properties - Select Builders - Select New - Select Ant Build - In the Main tab, complete the path to the build.xml file in the bin folder.

Check the Output

The Eclipse output window (named Console) should show the following after a build:

Buildfile: /home/<user>/src/Test/build.xml

CreateJar:
         [jar] Building jar: /home/<user>/src/Test/Test.jar
BUILD SUCCESSFUL
Total time: 152 milliseconds

Problem

I have an Eclipse Java project. It contains a folder named "dist". In that folder is a .jar file. How can I set things up in this project to make sure this .jar file is updated any time one of the .java files in the project has been re-compiled? Thanks.

Original source