Build Project into a Jar Automatically in Eclipse

Build project into a JAR automatically in Eclipse

Check out Apache Ant

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

How to create a jar with external libraries included in Eclipse?

When you export your project as a 'Runnable jar' (Right mouse on project -> Export -> Runnable jar) you have the option to package all dependencies into the generated jar. It also has two other ways (see screenshot) to export your libraries, be aware of the licences when deciding which packaging method you will use.

Package libraries

The 'launch configuration' dropdown is populated with classes containing a main(String[]) method. The selected class is started when you 'run' the jar.

Exporting as a runnable jar uses the dependencies on your build path (Right mouse on project -> Build Path -> Configure Build Path...). When you export as a 'regular' (non-runnable) jar you can select any file in your project(s). If you have the libraries in your project folder you can include them but external dependencies, for example maven, cannot be included (for maven projects, search here).

Compile a project in eclipse, get a war or ear or jar file

Right click on your project > Export > Runnable JAR file or just JAR file.

From there you have many options concerning libraries: extract them, pack them, or just put them aside the generated jar.

Eclipse: Export .JAR on save

Your best bet is:

1) Create an Ant script to compile your Java and export your .jar

2) Configure an Eclipse "Build" script to invoke Ant each file save.

Here are two links that tell you how:

  • eclipse: auto build after save

  • http://www.eclipsezone.com/eclipse/forums/t65486.html

Here's an excellent, brief tutorial for getting started with Ant:

  • http://www.vogella.com/tutorials/ApacheAnt/article.html

How to import a jar in Eclipse?

You can add a jar in Eclipse by right-clicking on the Project → Build Path → Configure Build Path. Under Libraries tab, click Add Jars or Add External JARs and give the Jar. A quick demo here.

Sample Image

The above solution is obviously a "Quick" one. However, if you are working on a project where you need to commit files to the source control repository, I would recommend adding Jar files to a dedicated library folder within your source control repository and referencing few or all of them as mentioned above.



Related Topics



Leave a reply



Submit