Build project into a JAR automatically in Eclipse
Asked Answered
A

7

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.

Annal answered 30/6, 2009 at 10:29 Comment(3)
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.Afresh
For 2018 use a build tool instead of IDE specific functionality. Maven buys you IDE independence and command line support.Lamella
@Craig Ringer - is there a link regarding how to do that?Womanhater
I
27

Check out Apache Ant

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

Idolater answered 30/6, 2009 at 10:40 Comment(6)
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.Annal
Apologies, I didn't go through the article in link earlier. It indeed talks about what I need. Thanks.Annal
Answer by Konrad more closely matches what the OP was looking for.Moriarty
lycono, I disagree - the OP wanted the jar kept up to date, and exporting a jar file doesn't try to solve that problem.Apollonius
In July 2011, the article referenced by @Peter doesn't talk about how to build a jar file at all.Apollonius
And now it's gone... That's why you should repeat the essential parts of the information from the linked resource in your answer.Ordnance
B
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.

Buyers answered 30/6, 2009 at 21:39 Comment(8)
Sad that this answer didn't get more upvotes (or best answer) months ago. Oh well, by now Konrad is gone.Wits
In Eclipse Galileo, I had to replace your step 1 with File -> Export -> Java -> JAR file.Duffel
Is there a way to automate the 'right click -> Create JAR' step?Pretorius
Is there a way to automate the 'right click -> Create JAR' step or bind it to a key combo?Windage
I use "Export... -> Java -> Runnable JAR file" to create exactly that.Association
Hmm I can create the jar but it won't run.Staphylo
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.Bullard
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".Nephrotomy
I
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

Impoverish answered 30/8, 2012 at 15:24 Comment(8)
But will it create the jar file each time I change any .java file (incrementally)?Atwood
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 :)Impoverish
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: <jar jarfile="dist/mypackage.jar" basedir="bin/" includes="**/*.class" />Operation
+1 How isn't this the accepted answer and not even the most up voted one?Carin
Thank you very much. I would recomment to not to have build.xml in src folder, but the rest is working fine, amazing !Maidstone
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.Murphree
@Murphree 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 :)Impoverish
@Maidstone 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.Impoverish
I
27

Check out Apache Ant

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

Idolater answered 30/6, 2009 at 10:40 Comment(6)
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.Annal
Apologies, I didn't go through the article in link earlier. It indeed talks about what I need. Thanks.Annal
Answer by Konrad more closely matches what the OP was looking for.Moriarty
lycono, I disagree - the OP wanted the jar kept up to date, and exporting a jar file doesn't try to solve that problem.Apollonius
In July 2011, the article referenced by @Peter doesn't talk about how to build a jar file at all.Apollonius
And now it's gone... That's why you should repeat the essential parts of the information from the linked resource in your answer.Ordnance
H
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.

Hsining answered 30/6, 2009 at 10:46 Comment(0)
O
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.

Orlene answered 30/1, 2012 at 19:51 Comment(0)
H
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.)

Hurdle answered 30/10, 2015 at 15:17 Comment(0)
P
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

Protest answered 15/2, 2016 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.