Packages & Modules
JAR Files
Analyze Java Archive packaging, manifest metadata manifest properties, Main-Class configurations, and archive compilation.
Interview: Evaluates manifest manifest file configurations, defining execution entry points (Main-Class), and command-line jar creation options.
A JAR (Java Archive) file is a ZIP-compressed archive used to package compiled class files, resources, and metadata into a single executable or library file, simplifying distribution.
ZIP Compression
JAR files use standard ZIP compression to package classes, images, and static resources into a single file.
The Manifest
The metadata file META-INF/MANIFEST.MF defines jar properties, classpath dependencies, and execution entry points.
Main-Class
Specifying the Main-Class property in the manifest allows you to run the JAR directly using java -jar app.jar.
The Role of MANIFEST.MF
The manifest file resides inside the META-INF folder and contains key-value pairs that configure the JAR:
- Main-Class: Defines the entry point class containing the
public static void main(String[] args)method. - Class-Path: Lists relative paths to other dependent JAR files required at runtime.
- Trailing Newline: IMPORTANT: The JavaBeans manifest parser requires a trailing newline at the end of the file. If omitted, the final line is ignored, which often breaks execution if
Main-Classis declared on that line.
Common Pitfalls
- Missing Trailing Newline in Manifest: Writing a custom manifest file without a trailing newline, causing the JVM to ignore the
Main-Classdirective and throw: "no main manifest attribute". - Mismatched Package Structures: Packing class files without preserving their package folder hierarchies, preventing the classloader from finding them inside the archive.
Best Practices
- Automate packaging: Use build tools like Maven or Gradle to compile, test, and package JARs instead of using manual command-line commands.
- Build Fat/Uber JARs for Services: For standalone applications, use tools (like the Maven Shade Plugin) to merge dependencies into a single "Fat JAR" for simple deployment.
Interview-Relevant Information
Q1: How do you make a JAR file executable?
Answer: Create a manifest file containing the directive Main-Class: com.company.Main followed by a trailing newline. Use the jar tool to package the classes and manifest: jar -cvfm app.jar manifest.txt -C bin/ .. The jar can then be run using java -jar app.jar.
Q2: Why does the JVM fail with "no main manifest attribute" when trying to run an executable JAR?
Answer: This error occurs if the manifest file inside the JAR does not define the Main-Class property, or if the compiler/manifest parser skipped the line because it lacked a trailing newline character.
Quick Checklist
Can you list the key-value pairs inside a standard manifest file, compile and package an executable JAR using the command line, and explain why trailing newlines are required in manifests? If yes, you understand JAR files.
Use Cases
Distributing reusable library modules to other developers (e.g. loggers, math libraries).
Packaging backend applications for simple deployment in server environments.
Common Mistakes
Forgetting the trailing blank line in manifest configurations, causing the JVM to miss the Main-Class property.
Attempting to run a library JAR without dependencies on the classpath, resulting in NoClassDefFoundErrors.