<p>Is there a tool or script which easily merges a bunch of <a href="http://en.wikipedia.org/wiki/JAR_%28file_format%29" rel="noreferrer">JAR</a> files into one JAR file? A bonus would be to easily set the main-file manifest and make it executable.</p>

<p>The concrete case is a <a href="http://jrst.labs.libre-entreprise.org/en/user/functionality.html" rel="noreferrer">Java restructured text tool</a>. I would like to run it with something like:</p>

<blockquote>
  <p>java -jar rst.jar</p>
</blockquote>

<p>As far as I can tell, it has no dependencies which indicates that it shouldn't be an easy single-file tool, but the downloaded ZIP file contains a lot of libraries.</p>

<pre><code>      0  11-30-07 10:01   jrst-0.8.1/
    922  11-30-07 09:53   jrst-0.8.1/jrst.bat
    898  11-30-07 09:53   jrst-0.8.1/jrst.sh
   2675  11-30-07 09:42   jrst-0.8.1/readmeEN.txt
 108821  11-30-07 09:59   jrst-0.8.1/jrst-0.8.1.jar
   2675  11-30-07 09:42   jrst-0.8.1/readme.txt
      0  11-30-07 10:01   jrst-0.8.1/lib/
  81508  11-30-07 09:49   jrst-0.8.1/lib/batik-util-1.6-1.jar
2450757  11-30-07 09:49   jrst-0.8.1/lib/icu4j-2.6.1.jar
 559366  11-30-07 09:49   jrst-0.8.1/lib/commons-collections-3.1.jar
  83613  11-30-07 09:49   jrst-0.8.1/lib/commons-io-1.3.1.jar
 207723  11-30-07 09:49   jrst-0.8.1/lib/commons-lang-2.1.jar
  52915  11-30-07 09:49   jrst-0.8.1/lib/commons-logging-1.1.jar
 260172  11-30-07 09:49   jrst-0.8.1/lib/commons-primitives-1.0.jar
 313898  11-30-07 09:49   jrst-0.8.1/lib/dom4j-1.6.1.jar
1994150  11-30-07 09:49   jrst-0.8.1/lib/fop-0.93-jdk15.jar
  55147  11-30-07 09:49   jrst-0.8.1/lib/activation-1.0.2.jar
 355030  11-30-07 09:49   jrst-0.8.1/lib/mail-1.3.3.jar
  77977  11-30-07 09:49   jrst-0.8.1/lib/servlet-api-2.3.jar
 226915  11-30-07 09:49   jrst-0.8.1/lib/jaxen-1.1.1.jar
 153253  11-30-07 09:49   jrst-0.8.1/lib/jdom-1.0.jar
  50789  11-30-07 09:49   jrst-0.8.1/lib/jewelcli-0.41.jar
 324952  11-30-07 09:49   jrst-0.8.1/lib/looks-1.2.2.jar
 121070  11-30-07 09:49   jrst-0.8.1/lib/junit-3.8.1.jar
 358085  11-30-07 09:49   jrst-0.8.1/lib/log4j-1.2.12.jar
  72150  11-30-07 09:49   jrst-0.8.1/lib/logkit-1.0.1.jar
 342897  11-30-07 09:49   jrst-0.8.1/lib/lutinwidget-0.9.jar
2160934  11-30-07 09:49   jrst-0.8.1/lib/docbook-xsl-nwalsh-1.71.1.jar
 301249  11-30-07 09:49   jrst-0.8.1/lib/xmlgraphics-commons-1.1.jar
  68610  11-30-07 09:49   jrst-0.8.1/lib/sdoc-0.5.0-beta.jar
3149655  11-30-07 09:49   jrst-0.8.1/lib/xalan-2.6.0.jar
1010675  11-30-07 09:49   jrst-0.8.1/lib/xercesImpl-2.6.2.jar
 194205  11-30-07 09:49   jrst-0.8.1/lib/xml-apis-1.3.02.jar
  78440  11-30-07 09:49   jrst-0.8.1/lib/xmlParserAPIs-2.0.2.jar
  86249  11-30-07 09:49   jrst-0.8.1/lib/xmlunit-1.1.jar
 108874  11-30-07 09:49   jrst-0.8.1/lib/xom-1.0.jar
  63966  11-30-07 09:49   jrst-0.8.1/lib/avalon-framework-4.1.3.jar
 138228  11-30-07 09:49   jrst-0.8.1/lib/batik-gui-util-1.6-1.jar
 216394  11-30-07 09:49   jrst-0.8.1/lib/l2fprod-common-0.1.jar
 121689  11-30-07 09:49   jrst-0.8.1/lib/lutinutil-0.26.jar
  76687  11-30-07 09:49   jrst-0.8.1/lib/batik-ext-1.6-1.jar
 124724  11-30-07 09:49   jrst-0.8.1/lib/xmlParserAPIs-2.6.2.jar
</code></pre>

<p>As you can see, it is somewhat desirable to not need to do this manually.</p>

<p>So far I've only tried AutoJar and ProGuard, both of which were fairly easy to get running. It appears that there's some issue with the constant pool in the JAR files.</p>

<p>Apparently jrst is slightly broken, so I'll make a go of fixing it. The <a href="http://en.wikipedia.org/wiki/Apache_Maven" rel="noreferrer">Maven</a> <code>pom.xml</code> file was apparently broken too, so I'll have to fix that before fixing jrst ... I feel like a bug-magnet :-)</p>

<hr>

<p>Update: I never got around to fixing this application, but I checked out <a href="http://en.wikipedia.org/wiki/Eclipse_%28software%29" rel="noreferrer">Eclipse</a>'s "Runnable JAR export wizard" which is based on a fat JAR. I found this very easy to use for deploying my own code.</p>

<p>Some of the other excellent suggestions might be better for builds in a non-Eclipse environment, oss probably should make a nice build using <a href="http://en.wikipedia.org/wiki/Apache_Ant" rel="noreferrer">Ant</a>. (Maven, so far has just given me pain, but others love it.)</p>

[{'answer_id': 81273, 'author': 'larsivi', 'author_id': 14047, 'author_profile': 'https://Stackoverflow.com/users/14047', 'pm_score': 4, 'selected': False, 'text': '<p>If you are a <a href="http://en.wikipedia.org/wiki/Apache_Maven" rel="nofollow noreferrer">Maven</a> user, typically the assembly plugin do what you want, or potentially the shade plugin, and in some cases a combination.</p>\n\n<p>With the assembly plugin you put a manifest file in your project with any necessary settings, although the defaults are usually quite good. Building is then done with</p>\n\n<pre><code>mvn assembly:assembly\n</code></pre>\n\n<p>Or if you have more special things to deal with, one of the other goals. All JAR files to include, are picked up by Maven\'s dependency resolver. If you use the shade plugin, it is typically part of the install goal, and in one particular project I\'m doing now I do</p>\n\n<pre><code>mvn install\nmvn assembly:single\n</code></pre>\n\n<p>The <code>assembly:single</code> goal is to work around lifetime issues, in this case in a <a href="http://en.wikipedia.org/wiki/Spring_Framework" rel="nofollow noreferrer">Spring</a> application.</p>\n'}, {'answer_id': 81277, 'author': 'PaulF', 'author_id': 12041, 'author_profile': 'https://Stackoverflow.com/users/12041', 'pm_score': -1, 'selected': False, 'text': '<p>Sounds like <a href="http://ant.apache.org/" rel="nofollow noreferrer">Apache Ant</a> is what you\'re looking for.</p>\n'}, {'answer_id': 81282, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>There is a tool called <a href="http://sourceforge.net/projects/autojar/" rel="nofollow noreferrer">autojar</a> which will scan your bytecode and compile a .jar file with the classes it finds, including referenced (imported) classes.</p>\n\n<p>Doesn\'t always work with something like Spring, though, where you specify the classnames in configuration and it gets loaded by the framework.</p>\n'}, {'answer_id': 81336, 'author': 'Stephen Denne', 'author_id': 11721, 'author_profile': 'https://Stackoverflow.com/users/11721', 'pm_score': 6, 'selected': True, 'text': "<p>Eclipse 3.4 JDT's Runnable JAR export wizard.</p>\n\n<p>In Eclipse 3.5, this has been extended. Now you can chose how you want to treat your referenced JAR files.</p>\n"}, {'answer_id': 81482, 'author': 'Ashley Mercer', 'author_id': 13065, 'author_profile': 'https://Stackoverflow.com/users/13065', 'pm_score': 5, 'selected': False, 'text': '<p>Having tried a few different solutions, I found <a href="http://one-jar.sourceforge.net/" rel="noreferrer">One-JAR</a>  the easiest to work with, and have managed to make do exactly that: produce a single, executable JAR which contains everything I need.</p>\n\n<p>One-JAR uses a custom class-loader which can navigate nested resources. Look at the .bat file in the download, it looks like org.codelutin.jrst.JRST in the jrst-0.8.1.jar is the main class, so your manifest should look like this:</p>\n\n<pre><code>Main-Class: com.simontuffs.onejar.Boot\nOne-Jar-Main-Class: org.codelutin.jrst.JRST\n</code></pre>\n\n<p>The really cool thing is that One-JAR will handle passing on command-line arguments for you. The classpath is handled by the custom class loader, assuming all the resources you need are bundled into the single JAR.</p>\n\n<p>The easiest way to use One-JAR is with ant; there\'s a custom "one-jar" ant task which works as follows (assuming your manifest is called "rst.mf"):</p>\n\n<pre><code>&lt;target name="jar-rst"&gt;\n    &lt;one-jar destfile="rst.jar" manifest="rst.mf"&gt;\n        &lt;main jar="jrst-0.8.1.jar" /&gt;\n        &lt;lib&gt;\n            &lt;fileset dir="${pathToJars}"&gt;\n                &lt;include name="batik-util-1.6-1.jar" /&gt;\n                &lt;include name="icu4j-2.6.1.jar" /&gt;\n                &lt;include name="commons-collections-3.1.jar" /&gt;\n                &lt;!-- Snip --&gt;\n            &lt;/fileset&gt;\n        &lt;/lib&gt;\n    &lt;/one-jar&gt;\n&lt;/target&gt;\n</code></pre>\n'}, {'answer_id': 81509, 'author': 'jrudolph', 'author_id': 7647, 'author_profile': 'https://Stackoverflow.com/users/7647', 'pm_score': 3, 'selected': False, 'text': '<p>There is <a href="http://proguard.sourceforge.net/" rel="nofollow noreferrer">ProGuard</a> which does not only pack your JAR files into one, but it can also optimize, cleanup or obfuscate your class files, making the resulting JAR file much smaller than the sum of all JAR files before.</p>\n\n<p>I actually tried ProGuard with the JRST tool, and it is as you reported. I tried to track the problem down and found it to relate to <a href="http://bugs.icu-project.org/trac/ticket/3209" rel="nofollow noreferrer">a bug</a> in the ICU4J library referenced by jrst. The problem is, that the used ICU version is far outdated right now. So I replaced the <code>icu.jar</code> file with ICU4J version 3.2. Now ProGuard finds a bunch of other errors/warnings about incosistencies with the libraries of JRST.</p>\n\n<p>My guess is that ProGuard works as expected, but the libraries of jrst are just not consistent. I don\'t know if you can do much more than talk with its developers since they should check and update the dependencies of the project.</p>\n'}, {'answer_id': 81519, 'author': 'Philip Helger', 'author_id': 15254, 'author_profile': 'https://Stackoverflow.com/users/15254', 'pm_score': 2, 'selected': False, 'text': '<p>Or using the Maven assembly plugin (mvn assembly:assembly)</p>\n'}, {'answer_id': 115805, 'author': 'Steve g', 'author_id': 12092, 'author_profile': 'https://Stackoverflow.com/users/12092', 'pm_score': 3, 'selected': False, 'text': '<p>You can use <a href="http://code.google.com/p/jarjar/" rel="nofollow noreferrer">JarJar</a> which will use package shadowing to make sure your JAR file doesn\'t conflict with others.</p>\n'}, {'answer_id': 185116, 'author': 'akuhn', 'author_id': 24468, 'author_profile': 'https://Stackoverflow.com/users/24468', 'pm_score': 6, 'selected': False, 'text': '<p>Ant\'s <code>zipfileset</code> does the job</p>\n\n<pre><code>&lt;jar id="files" jarfile="all.jar"&gt;\n    &lt;zipfileset src="first.jar" includes="**/*.java **/*.class"/&gt;\n    &lt;zipfileset src="second.jar" includes="**/*.java **/*.class"/&gt;\n&lt;/jar&gt;\n</code></pre>\n'}, {'answer_id': 3270607, 'author': 'simontuffs', 'author_id': 394525, 'author_profile': 'https://Stackoverflow.com/users/394525', 'pm_score': 3, 'selected': False, 'text': '<p>One-JAR 0.97 has just been released at <a href="http://one-jar.sourceforge.net" rel="nofollow noreferrer">http://one-jar.sourceforge.net</a>, and it has been extended wih support for frameworks such as <a href="http://en.wikipedia.org/wiki/Spring_Framework" rel="nofollow noreferrer">Spring</a> and <a href="http://en.wikipedia.org/wiki/Google_Guice" rel="nofollow noreferrer">Guice</a>, which may present trouble to other approaches. It also handles classloader-inversion -- where some JAR files are external to the One-JAR (for example, <a href="http://en.wikipedia.org/wiki/Java_Database_Connectivity" rel="nofollow noreferrer">JDBC</a> drivers which may not be shipped bundled).</p>\n\n<p>One-JAR is command-line, with <a href="http://en.wikipedia.org/wiki/Apache_Ant" rel="nofollow noreferrer">Ant</a> and <a href="http://en.wikipedia.org/wiki/Apache_Maven" rel="nofollow noreferrer">Maven</a> 2 plugins. It\'s also simple to build just using the "jar" tool.</p>\n\n<p>I can also recommend the Eclipse Jar Exporter (Runnable) on which Ference Hechler wrote: he did a great job in coming up with a simple approach to wrapping a set of JAR files. He and I worked on One-JAR, but the Jar Exporter is based on a different codebase.</p>\n'}, {'answer_id': 5652720, 'author': '象嘉道', 'author_id': 571828, 'author_profile': 'https://Stackoverflow.com/users/571828', 'pm_score': 3, 'selected': False, 'text': '<p>(based on Andrian\'s):</p>\n\n<pre><code>&lt;jar id="files" jarfile="all.jar"&gt;\n  &lt;zipgroupfileset dir="${library.dir}" includes="*.jar" excludes="test-helper.jar"/&gt;\n  &lt;zipfileset src="first.jar" includes="**/*.java **/*.class"/&gt;\n  &lt;zipfileset src="second.jar" includes="**/*.java **/*.class"/&gt;\n  &lt;fileset dir="."&gt;\n    &lt;include name="LICENSE"/&gt;\n    &lt;include name="NOTICE"/&gt;\n  &lt;/fileset&gt;\n&lt;/jar&gt;\n</code></pre>\n'}, {'answer_id': 24189504, 'author': 'hoang', 'author_id': 2027716, 'author_profile': 'https://Stackoverflow.com/users/2027716', 'pm_score': 0, 'selected': False, 'text': '<p>You should use maven shading plugin to do that. I often use maven to build standalone jar file and it\'s so powerful</p>\n\n<p>See more:</p>\n\n<p><a href="http://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html" rel="nofollow">http://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html</a></p>\n'}, {'answer_id': 30698416, 'author': 'Nadir', 'author_id': 1641763, 'author_profile': 'https://Stackoverflow.com/users/1641763', 'pm_score': 2, 'selected': False, 'text': '<p>I think that the tool you need here is <strong>JarSplice</strong>: <a href="http://ninjacave.com/jarsplice" rel="nofollow">http://ninjacave.com/jarsplice</a></p>\n\n<p>It does <strong>not</strong> require Ant or Maven, has its own GUI, it is straightforward to use and do exactly what you asked --> <strong><em>It Merges the content of several jar files into a single one</em></strong> (please note it still need to add its own classloader).</p>\n'}]
['https://Stackoverflow.com/questions/81260', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/12677/']