Can i combine many jar files in one jar file

前端 未结 8 2014
囚心锁ツ
囚心锁ツ 2020-12-18 11:07

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?

相关标签:
8条回答
  • 2020-12-18 11:10

    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.

    0 讨论(0)
  • 2020-12-18 11:13

    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.

    0 讨论(0)
  • 2020-12-18 11:27

    There are a number of existing tools for this:

    • Uberjar
    • Megajar
    • Onejar
    0 讨论(0)
  • 2020-12-18 11:30

    The jarjar project is what you need.

    0 讨论(0)
  • 2020-12-18 11:31

    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>
    
    0 讨论(0)
  • 2020-12-18 11:31

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

    0 讨论(0)
提交回复
热议问题