What is a fat JAR?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
A fat JAR, also known as an uber JAR, is a Java ARchive (JAR) file that contains not only the application code but also all of its dependencies and libraries. This approach allows for a single deployable unit that contains everything necessary to run the application, thus simplifying deployment and execution processes.
Understanding Fat JARs
JAR Basics
At its core, a JAR file is a package file format used to aggregate multiple Java class files and associated resources such as text, images, etc., into one file. It's a way to distribute and deploy Java applications in a compressed format that can be executed by the Java Runtime Environment (JRE).
What Makes a Fat JAR "Fat"?
Unlike a regular JAR file that typically contains only application-specific classes, a fat JAR consolidates all dependent libraries and resources into a single archivable file. This means that all the third-party libraries that the application depends on are included. Essentially, a fat JAR is self-contained and can be executed independently without worrying about external dependencies.
Technical Explanation
When you create and execute a fat JAR, the Java class loader can locate all the necessary classes directly from that single file, which simplifies classpath management. Fat JARs include a manifest file with the list of all required resources and libraries, making it easier to launch the JAR with a simple command like:
This format is particularly useful when deploying applications in environments where dependency management might be cumbersome, such as Docker containers, cloud platforms, or microservice architectures.
Tools for Creating Fat JARs
Creating a fat JAR is a straightforward process, primarily aided by build tools and plugins. Here are a few popular methods:
- Maven: The Maven shade plugin is often used to create a fat JAR. It packages the artifact in an uber JAR after resolving all dependencies.
- Gradle: Gradle uses the Shadow Plugin to produce fat JARs. It simplifies the task of dependency management and builds a comprehensive JAR file.
- Spring Boot: If you are using Spring Boot, the Spring Boot Maven/Gradle plugins have embedded support for packaging applications as fat JARs.
Example: Creating a Fat JAR with Maven
Here's a simple setup to create a fat JAR using the Maven shade plugin:
This setup will create a fat JAR when you execute the mvn package command.
Advantages of Fat JARs
- Simplified Deployment: With all dependencies packaged together, it reduces the complexities of environment-specific dependency issues.
- Ease of Distribution: It is easier to distribute a single file that contains everything necessary for execution.
- Consistent Environment: As the dependencies are packaged, you ensure that every execution environment uses the exact version of libraries, reducing inconsistencies and errors.
- Microservices and Containerization: Ideal for containerized and microservices environments where each service can be isolated with its dependencies.
Disadvantages of Fat JARs
- Size Increase: Including all dependencies can significantly increase the file size, making it cumbersome for systems with limited resources.
- Redundancy: If multiple applications use common libraries, each fat JAR would contain a copy of these libraries, leading to redundancy.
- Class Conflicts: There might be potential for classpath conflicts if different libraries provide incompatible versions of the same class.
Key Points Summary
| Aspect | Description |
| Definition | A JAR that includes the application code and its dependencies. |
| Usage | Common in microservices, Docker, and cloud environments for ease of deployment. |
| Tools | Commonly built using tools like Maven, Gradle, and Spring Boot plugins. |
| Advantages | Simplifies deployment, consistent environments, ease of distribution. |
| Disadvantages | Increased size, redundancy, potential for classpath conflicts. |
Conclusion
A fat JAR provides an effective means of packaging Java applications by bundling all necessary components into a single executable artifact. While convenient and simplifying the execution environment, developers should be mindful of its increased size, potential redundancies, and classpath conflict issues. Nonetheless, it remains a popular choice in scenarios requiring high consistency and simplified deployment, particularly in dynamic environments like cloud and microservices architectures.
Related reading
- What is a good Java library to zip/unzip files?
- What is a Java ClassLoader?
- What is a listener container in Spring for Apache Kafka?
- What is a Maven artifact?
- What is a non recursive solution for Fibonacci-like sequence in Java?
- What is a NullPointerException, and how do I fix it?
- What is a raw type and why shouldn't we use it?
- What is a safe way to create a Temp file in Java?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.