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

£可爱£侵袭症+ 提交于 2019-11-27 17:58:28

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

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.)

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

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

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.

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.

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]

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!