John
John

Reputation:

Can i combine many jar files in one jar file

I have multiple JAR files, which I have to add to classpath in Eclipse.

Is it possible to combine 30 files in one file and include that file?

Upvotes: 3

Views: 19134

Answers (8)

AlexR
AlexR

Reputation: 115388

First, you can. JAR is just a ZIP file. You can extract all stuff into one directory, then create zip file (even manually using WinZip or similar tool), call the result *.jar (if you wish) and add it into the classpath.

The only problem that will probably happen is if several jar files contain resources with the same name (and path). for example, manfest.mf, or other descriptors. In most cases it will not cause any problem but sometimes if application code uses these resources you can have trouble.

There are some automatic tools that do this. Take a look on JarJar.

BUT the more important question: why do you want to do this? If you do not use maven put all library jars to one directory named lib under your project. Then add all these jars to your classpath in eclipse using 2-3 clicks. That's it.

if you are using maven, include all your direct dependences into your pom.xml, compile the project, then say mvn eclipse:eclipse. This will create .classspath and .project for you. Now just use it.

Upvotes: 1

Patrick
Patrick

Reputation: 849

There is another thread here that explains how to package jars for releases, including using Ant, jarjar and eclipse plugins.

Upvotes: 1

Chris
Chris

Reputation: 8024

Your may want to have a look at jarjar.

If you use an ant task you can also go for zipgroupfileset:

<zip destfile="jarlibrary.jar">
    <zipgroupfileset dir="lib" includes="*.jar"/>
</zip>

Upvotes: 6

crowne
crowne

Reputation: 8534

There are a number of existing tools for this:

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1503290

You can, but I don't think it would necessarily be a good idea to do so. Three possible reasons, and no doubt there are more:

  • It makes it harder to see where any one constituent file comes from.
  • It makes it harder to replace just one library
  • If there are files which are contained in multiple jar files, which should "win"? For example, all the jar files will have their own manifest... you may be able to merge them all, but not necessarily... and for other files there may well simply not be a sensible way of merging them.

My vote is to keep the jar files separate.

Upvotes: 7

Umesh Kacha
Umesh Kacha

Reputation: 13686

This question seems probable duplicate. Please try to find answer from similar article in this forum Clean way to combine multiple jars

Upvotes: 2

Tim B&#252;the
Tim B&#252;the

Reputation: 63794

The jarjar project is what you need.

Upvotes: 1

Sjoerd
Sjoerd

Reputation: 75659

Jar files are just ZIP files, so you can try to unzip all jars, put all files together and then ZIP them again.

Upvotes: 0

Related Questions