3

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?

Kshitiz Sharma
  • 17,947
  • 26
  • 98
  • 169
  • possible duplicate of [Clean way to combine multiple jars? Preferably using ant](http://stackoverflow.com/questions/515428/clean-way-to-combine-multiple-jars-preferably-using-ant) – ripper234 Feb 23 '11 at 10:26
  • Answered here: http://stackoverflow.com/questions/5080220/how-to-combine-two-jar-files – karma Aug 07 '15 at 06:25

8 Answers8

7

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I would argue against the "It makes it harder to replace just one library" point, since you could combine them in your build process but keep and manage them separately. – Tim Büthe Feb 23 '11 at 10:28
  • 1
    @Tim - that doesn't help if you are consumer of some project that only provides an "uberjar". – Stephen C Feb 23 '11 at 10:35
  • 1
    @Stephen C: True, but we really don't know the scenario here. Maybe, he isn't providing a lib for other projects to use. If he does, a maven like dependency management are a much better fit. – Tim Büthe Feb 23 '11 at 10:43
6

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>
Chris
  • 7,864
  • 1
  • 27
  • 38
3

There are a number of existing tools for this:

crowne
  • 8,456
  • 3
  • 35
  • 50
2

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

Community
  • 1
  • 1
Umesh K
  • 13,436
  • 25
  • 87
  • 129
1

The jarjar project is what you need.

Tim Büthe
  • 62,884
  • 17
  • 92
  • 129
1

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

Community
  • 1
  • 1
Patrick
  • 809
  • 3
  • 9
  • 21
1

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.

AlexR
  • 114,158
  • 16
  • 130
  • 208
0

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

Sjoerd
  • 74,049
  • 16
  • 131
  • 175