Creating an UberJAR for an application and its dependencies
suggest changeA common requirement for a Java application is that it can be deployed by copying a single file. For simple applications that depend only on the standard Java SE class libraries, this requirement is satisfied by creating a JAR file containing all of the (compiled) application classes.
Things are not so straightforward if the application depends on third-party libraries. If you simply put dependency JAR files inside an application JAR, the standard Java class loader will not be able to find the library classes, and your application will not start. Instead, you need to create a single JAR file that contains the application classes and associated resources together with the dependency classes and resources. These need to be organized as a single namespace for the classloader to search.
Such a JAR file is often called an UberJAR.
Creating an UberJAR using the "jar" command
The procedure for creating an UberJAR is straight-forward. (I will use Linux commands for simplicity. The commands should be identical for Mac OS, and similar for Windows.)
-
Create a temporary directory, and change directory to it.
$ mkdir tempDir $ cd tempDir -
For each dependency JAR, use
jarto unpack it into the temporary directory, in the reverse of classpath order. The last unpacked JAR wins when paths collide, so unpack the first classpath entry last.$ jar -xf <path/to/file.jar>Doing this for multiple JAR files overlays their contents. A later JAR overwrites an earlier
META-INF/MANIFEST.MF. -
Copy the application classes from the build tree into the temporary directory.
$ cp -r path/to/classes . -
Create the UberJAR from the contents of the temporary directory:
$ jar -cf ../myApplication.jar .If you are creating an executable JAR, include a
MANIFEST.MFthat setsMain-Class. Put that manifest in the temporary directory before this command, or pass it withjar -cfm.
Creating an UberJAR using Maven
If your project is built using Maven, you can get it to create an UberJAR using either the "maven-assembly" or "maven-shade" plugins. See the Maven Assembly topic (in the Maven documentation) for details.
The advantages and drawbacks of UberJARs
Some advantages of UberJARs are self-evident:
- An UberJAR is easy to distribute.
- You cannot break the library dependencies for an UberJAR, since the libraries are self-contained.
In addition, UberJAR tools can exclude library classes that appear unused. That exclusion is typically done by static analysis. If the application uses reflection, annotation processing, or similar techniques, classes can be excluded incorrectly.
UberJARs also have some disadvantages:
-
If you have lots of UberJARs with the same dependencies, then each one will contain a copy of the dependencies.
-
Some open source libraries have licenses which may preclude their use in an UberJAR.
1
1 - Some open source library licenses allow you to use the library only if the end user can replace one version of the library with another. UberJARs can make that replacement difficult.