173

I have an Eclipse project where I want to keep my Java project built into a JAR automatically. I know I have an option to export the project into a JAR; if I do a right click; but what I am really looking for is, that like Eclipse automatically builds a project's .class files and put them in target folder; it should also build a JAR automatically and copy the latest JAR at some or a specific location.

Is there a option to configure Eclipse in such a way, to build JARs automatically?

Just to make it clear for guys, patient enough to answer my question; I am not looking at ANT as solution; as I already use it, but what I would like it something that gets initiated automatically either with a time based trigger or immediate build with change.

spongebob
  • 8,370
  • 15
  • 50
  • 83
Priyank
  • 14,231
  • 18
  • 78
  • 107
  • 1
    My personal opinion: Use m2e, Eclipse's maven integration, so it outputs a jar for you as part of the build. I recently tried to use Eclipse without Maven and can't believe how primitive the basic Java builder is. – Craig Ringer Oct 08 '14 at 04:58
  • 1
    For 2018 use a build tool instead of IDE specific functionality. Maven buys you IDE independence and command line support. – Thorbjørn Ravn Andersen Mar 19 '18 at 21:48
  • @Craig Ringer - is there a link regarding how to do that? – nsandersen Oct 30 '19 at 17:45

7 Answers7

293

You want a .jardesc file. They do not kick off automatically, but it's within 2 clicks.

  1. Right click on your project
  2. Choose Export > Java > JAR file
  3. Choose included files and name output JAR, then click Next
  4. Check "Save the description of this JAR in the workspace" and choose a name for the new .jardesc file

Now, all you have to do is right click on your .jardesc file and choose Create JAR and it will export it in the same spot.

spongebob
  • 8,370
  • 15
  • 50
  • 83
Konrad
  • 3,593
  • 3
  • 19
  • 17
  • Sad that this answer didn't get more upvotes (or best answer) months ago. Oh well, by now Konrad is gone. – Dan Rosenstark Jan 08 '10 at 11:52
  • 25
    In Eclipse Galileo, I had to replace your step 1 with File -> Export -> Java -> JAR file. – Matt Huggins Jul 15 '10 at 01:22
  • 9
    Is there a way to automate the 'right click -> Create JAR' step? – Asaf Dec 26 '11 at 15:15
  • 4
    Is there a way to automate the 'right click -> Create JAR' step or bind it to a key combo? – Pacerier Feb 20 '12 at 14:55
  • 5
    I use "Export... -> Java -> Runnable JAR file" to create exactly that. – TedTrippin Jan 10 '13 at 17:28
  • Hmm I can create the jar but it won't run. – Stephen__T Oct 29 '13 at 04:36
  • I didn't want to include the sources in the JAR, but I had to add the "src" folder in order to create a non-empty JAR. The curious thing is that when I select the "src" folder, the sources are not in the JAR. – user2518618 Jul 17 '14 at 12:28
  • Note that creating the JAR by double-clicking on the jardesc file and then clicking Finish, you'll be prompted to save the jardesc again, as it was originally created with the Save Description flag set. Easiest way around this is to manually edit it, and change `saveDescription="true"` to `saveDescription="false"`. – Steve Melnikoff Jan 15 '16 at 14:29
73

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

EDIT: Some helpful comments by @yeoman and @betlista

@yeoman I think the correct include would be /.class, not *.class, as most people use packages and thus recursive search for class files makes more sense than flat inclusion

@betlista I would recomment to not to have build.xml in src folder

Thomas Bratt
  • 48,038
  • 36
  • 121
  • 139
  • But will it create the jar file each time I change any .java file (incrementally)? – devoured elysium Sep 03 '12 at 09:03
  • 2
    Yes, you can choose the targets for the build step as follows:After a Clean, Manual Build, Auto Build and During a Clean. Auto Build should give you what you want :) – Thomas Bratt Sep 03 '12 at 16:22
  • 8
    After having tried both, this solution seems much better than Konrad's. It requires you to manually figure out some paths in your build file but it allows automatic build (instead of 2 clicks) and allow for easy customization (copy the jar after build for example). Here's how to build a jar file when having the build file in the root and having multiple nested levels of packages: `` – worldsayshi Dec 06 '12 at 08:56
  • 3
    +1 How isn't this the accepted answer and not even the most up voted one? – Juan Garcia Jul 27 '14 at 19:09
  • 2
    Thank you very much. I would recomment to not to have `build.xml` in src folder, but the rest is working fine, amazing ! – Betlista Feb 02 '16 at 09:24
  • 1
    I think the correct include would be **/**.class, not *.class, as most people use packages and thus recursive search for class files makes more sense than flat inclusion. Will remove my +1 until then because the answer doesn't usually work as it stands. – yeoman Feb 05 '17 at 07:41
  • @yeoman I've added your comments in a highlighted section at the bottom of the answer. I'm no longer an active Eclipse user so I'm not best placed to evaluate your comments or test the changes you suggest. Hope that helps :) – Thomas Bratt Feb 05 '17 at 11:05
  • @Betlista I've added your comment to my answer. Hope that covers it - don't really want to update the answer given that I won't be testing it. – Thomas Bratt Feb 05 '17 at 11:06
27

Check out Apache Ant

It's possible to use Ant for automatic builds with eclipse, here's how

  • I am already using Apache ant to do the builds and make Jar. But what I was really looking for was a way around apache ant, and directly using eclipse. – Priyank Jun 30 '09 at 10:42
  • Apologies, I didn't go through the article in link earlier. It indeed talks about what I need. Thanks. – Priyank Jun 30 '09 at 10:49
  • 1
    Answer by Konrad more closely matches what the OP was looking for. – lycono Feb 15 '11 at 01:07
  • lycono, I disagree - the OP wanted the jar kept up to date, and exporting a jar file doesn't try to solve that problem. – James Moore Apr 30 '11 at 19:41
  • 2
    In July 2011, the article referenced by @Peter doesn't talk about how to build a jar file at all. – James Moore Jul 27 '11 at 16:28
  • 2
    And now it's gone... That's why you should repeat the essential parts of the information from the linked resource in your answer. – thoni56 Jan 13 '21 at 08:40
13

This is possible by defining a custom Builder in eclipse (see the link in Peter's answer). However, unless your project is very small, it may slow down your workspace unacceptably. Autobuild for class files happens incrementally, i.e. only those classes affected by a change are recompiled, but the JAR file will have to be rebuilt and copied completely, every time you save a change.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
5

Regarding to Peter's answer and Micheal's addition to it you may find How Do I Automatically Generate A .jar File In An Eclipse Java Project useful. Because even you have "*.jardesc" file on your project you have to run it manually. It may cools down your "eclipse click hassle" a bit.

Community
  • 1
  • 1
underscore
  • 752
  • 12
  • 9
1

Using Thomas Bratt's answer above, just make sure your build.xml is configured properly :

<?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="bin/" includes="**/*.class" />
  </target>
</project>

(Notice the double asterisk - it will tell build to look for .class files in all sub-directories.)

Hugo Leote
  • 41
  • 4
0

Creating a builder launcher is an issue since 2 projects cannot have the same external tool build name. Each name has to be unique. I am currently facing this issue to automate my build and copy the JAR to an external location.

I am using IBM's Zip Builder, but that is just a help but not doing the real.

People can try using IBM ZIP Creation plugin. http://www.ibm.com/developerworks/websphere/library/techarticles/0112_deboer/deboer2.html#download