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

前端 未结 5 1723
失恋的感觉
失恋的感觉 2020-12-04 16:22

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 th

相关标签:
5条回答
  • 2020-12-04 17:02

    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

    0 讨论(0)
  • 2020-12-04 17:07

    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.

    0 讨论(0)
  • 2020-12-04 17:09

    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.

    0 讨论(0)
  • 2020-12-04 17:16

    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]

    0 讨论(0)
  • 2020-12-04 17:25

    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
    
    0 讨论(0)
提交回复
热议问题