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

5

47

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.

Zia answered 13/7, 2009 at 14:12 Comment(1)
duplicate of #1063441Palaeobotany
W
36

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. Build.xml in Eclipse Project

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.

Ant builder configuration Build step - Targets Tab

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
Wellknit answered 30/8, 2012 at 14:44 Comment(4)
This works great except I want to include all the dependencies as well! How would I do that?Metalanguage
I ask the same as @advocate. My jar file now has only 346 bytes, while it should have some 50kB!Monosyllable
try includes="**/*.class" to get the classes from the subfolders tooCluff
@Paramaeleon that was essential, should be in the answer!!Mousebird
E
17

You can define an Ant builder which runs a jar task to jar all the class files in the project (With "Refresh project upon completion" set.)

alt text

(See also "Customizing Builds for Your Eclipse Projects")

See IBM article: How and why to create custom Ant tasks

alt text

Eccentricity answered 13/7, 2009 at 14:48 Comment(0)
H
4

A common pattern is to work against the class files in the project (projects can be added to other projects build paths and used at runtime while testing), so you don't actually need the jar files during development.

The geeneral approach for adding an automatic build step is to write an ant script, include that in your project and you can then have the execution of your ant script included in the project's build. So as ant has a pretty straighforward jar building task this isn't too much effort if you do need the jar file all the time. See for a starter.

Heptameter answered 13/7, 2009 at 14:24 Comment(0)
R
2

Create a J2EE Utility project (Util). It lets you create an association with a J2EE project (ProjectX). When you edit the properties of ProjectX to depend upon the Util project, it shows Util as Util.jar. With the dependency declared, Eclipse will build the Util.jar when it has to build the Util project. If you have auto-build active for the Util project, the .jar file will be kept in sync each time the project is built. If your target project isn't a J2EE one, you can still use this solution but use a dummy J2EE parent project.

Here is the link to the help page for using the ANT task for building a .zip file from within Eclipse: http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.pde.doc.user/tasks/pde_feature_generating_ant.htm

An alternate solution is to use the Zip plugin. We used this over 5 years ago but stopped when WSAD included support for dependent projects as .jar files.

Regionalism answered 13/7, 2009 at 15:12 Comment(0)
S
0

Thomas's answer works, but the jar file it produces isn't one that you can use to actually run the application.

I ended up with:

<?xml version="1.0" ?>
<!-- Configuration of the Ant build system to generate a Jar file --> 
<project name="TDSz Data Mover" default="CreateJar">
  <target name="CreateJar" description="Create Jar file">
        <delete file="DataMover.jar"/>
        <jar jarfile="DataMover.jar" basedir="bin/" includes="**/*.class **/Messages*.*" " update="no">
            <zipfileset dir="D:/Java/mylib" erroronmissingarchive="true">
                <include name="*.jar" />
            </zipfileset>  
            <manifest>
                <attribute name="Main-Class" value="some.package.and.app"/>
            </manifest>             
        </jar>
  </target>
</project>

Don't know if something has changed in ant since this answer was given, but it took some digging to actually get it working. A lot of the solutions in tutorials were only partial answers...

Main changes:

  • Added a delete for the jar file as it wasn't regenerating it when I reran the ant build after changing the build file.
  • Added a manifest to set the executable file properly.
  • Pull in some .jar files as libs
  • Pull in the Message_ files for NLS support

Netbeans makes this so much easier - just check a couple of boxes.

[Edited to fix issue with incorrectly terminated jar tag and pull in the .jar files]

Stratagem answered 22/2, 2018 at 5:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.