is it possible to build a jar file made up of all jar files that I need? I am building my project and the jar files are not being included in the generated jar file and I would like to generate one separate jar file which contains all jar files in this project
Asked
Active
Viewed 2,975 times
1
-
take a look at this post: http://stackoverflow.com/questions/183292/classpath-including-jar-within-a-jar – dexametason Apr 05 '12 at 20:25
-
*"is it possible to build a jar file made up of all jar files that I need?"* It is generally better to include a manifest in the main Jar which identifies the dependent Jars by relative paths. Why do you not implement that strategy? – Andrew Thompson Apr 05 '12 at 20:51
5 Answers
0
Not possible. You can't include jars in a jar in order to have them on the classpath (you can put them in the jar, but you can't use them as dependencies)
Maven, however, gives you an alternative with the maven-assembly-plugin. It unpacks all jars and stores their .class files in your new, single jar. There are, of course, other options to do the same thing, but maven is a great build tool to use anyway.

Bozho
- 588,226
- 146
- 1,060
- 1,140
-
*"Not possible. You can't include jars in a jar."* Sure you can. Whether it can be made to work at run-time is a separate matter. On that matter, I ***believe*** there are also custom class loaders that can load resources from within Jar inside other Jars. – Andrew Thompson Apr 05 '12 at 20:54
-
yes, that's what I meant. You can put them in, of course, but you can't use them. The classloader is a good idea, do you have a link perhaps? – Bozho Apr 05 '12 at 21:27
-
I thought you might ask that. ;) A quick search on "jar class loader" shows ..a lot of dross, but in the 1st 5 hits is a link to .. 'One Jar', as mentioned by evanwong. *"One-JAR provides custom classloader that knows how to load classes and resources from a jars inside an archive, instead of from jars in the filesystem."* :) – Andrew Thompson Apr 05 '12 at 21:33
0
The yguard tool can do this. It can also shrink your code and obfuscate it if you wish.
Here is an ant example from their documentation:
<taskdef name="yguard" classname="com.yworks.yguard.YGuardTask" classpath="buildlib/yguard.jar"/>
<inoutpairs resources="auto">
<fileset dir="${input-lib-dir}">
<include name="myapp*.jar"/>
<exclude name="*_obf.jar"/>
</fileset>
<mapper type="glob" from="*.jar" to="*_obf.jar"/>
</inoutpairs>

Sarel Botha
- 12,419
- 7
- 54
- 59
0
Take a look at onejar: http://one-jar.sourceforge.net/

evanwong
- 5,054
- 3
- 31
- 44
-
One-JAR is no longer maintained. I am maintaining a partly modernized fork at https://github.com/nsoft/uno-jar – Gus May 05 '20 at 01:57
0
You can use one jar maven plug in.
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!-- REPOSITORIES -->
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<pluginRepositories>
<pluginRepository>
<id>onejar-maven-plugin.googlecode.com</id>
<url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
</pluginRepository>
</pluginRepositories>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!-- ONE JAR PLUGIN -->
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>org.acme.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.dstovall</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<configuration>
<!-- Optional -->
<onejarVersion>0.97</onejarVersion>
<!--
Optional, use only if you need to include native libraries
(dll's) <binlibs> <fileSet>
<directory>${project.build.directory}/dllextract</directory>
<includes> <include>test.dll</include> </includes> </fileSet>
</binlibs>
-->
<!-- Optional, default is false -->
<attachToBuild>true</attachToBuild>
<!-- Optional, default is "onejar" -->
<classifier>onejar</classifier>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>

Gesuino
- 26
- 3