What exactly does a jar file contain?
Categories:
Unpacking the JAR: What Exactly Does a Java Archive File Contain?

Explore the structure and contents of a Java Archive (JAR) file, understanding its role in Java application deployment and execution. Learn about manifest files, class files, resources, and digital signatures.
A Java Archive (JAR) file is a package file format used to aggregate many Java class files, associated metadata, and resources (text, images, etc.) into a single file for distribution. It's essentially a ZIP file with a .jar
extension, specifically designed for Java applications. Understanding what's inside a JAR is crucial for debugging, deployment, and optimizing Java projects.
The Core Components of a JAR File
At its heart, a JAR file is a standard ZIP archive. This means you can use any ZIP utility to open and inspect its contents. However, the specific structure and files within a JAR are what make it special for Java. The primary components include compiled Java class files, a manifest file, and various resource files.
flowchart TD A[JAR File] --> B["META-INF/MANIFEST.MF"] A --> C["Compiled Java Classes (.class)"] A --> D["Resource Files (images, properties, XML)"] A --> E["Optional: Digital Signatures"] A --> F["Optional: Other Metadata"] C --> C1["com/example/MyClass.class"] C --> C2["com/example/AnotherClass.class"] D --> D1["config.properties"] D --> D2["logo.png"] D --> D3["data.xml"] B --> B1["Main-Class: com.example.MainApp"] B --> B2["Class-Path: lib/dependency.jar"] B --> B3["Sealed: true"] E --> E1["META-INF/*.SF"] E --> E2["META-INF/*.DSA / *.RSA"] F --> F1["Service Provider Configuration"] F --> F2["Native Libraries"] subgraph JAR Structure B C D E F end
High-level structure and common contents of a JAR file.
1. Compiled Java Class Files (.class
)
These are the bytecode files generated by the Java compiler (javac
) from your Java source code (.java
files). When the Java Virtual Machine (JVM) executes a Java application, it loads and interprets these .class
files. They are typically organized in a directory structure that mirrors their package hierarchy.
jar tvf myapplication.jar | grep .class
# Example Output:
# 12345 Mon Jan 01 00:00:00 UTC 2023 com/example/MainApp.class
# 6789 Mon Jan 01 00:00:00 UTC 2023 com/example/util/Helper.class
Listing class files within a JAR using the jar
command.
2. The Manifest File (META-INF/MANIFEST.MF
)
Every JAR file contains a special directory named META-INF
, and within it, a crucial file called MANIFEST.MF
. This file acts as a central repository for metadata about the JAR's contents. It can specify the main class to be executed, classpath entries for other JARs, version information, digital signature details, and more. Without a properly configured manifest, the JVM might not know how to run an executable JAR.
Manifest-Version: 1.0
Created-By: 1.8.0_202 (Oracle Corporation)
Main-Class: com.example.MyApplication
Class-Path: lib/dependency1.jar lib/dependency2.jar
Sealed: true
Example content of a MANIFEST.MF
file.
Main-Class
entry in the manifest is essential for making a JAR executable. When you run java -jar your_app.jar
, the JVM looks for this entry to know which class contains the public static void main(String[] args)
method to start the application.3. Resource Files
JAR files can also bundle various non-code assets that your application needs. These are often referred to as resource files and can include images, configuration files (e.g., .properties
, .xml
, .json
), sound files, or any other data your application might require at runtime. These resources are typically accessed using ClassLoader.getResource()
or ClassLoader.getResourceAsStream()
methods.
InputStream input = getClass().getClassLoader().getResourceAsStream("config.properties");
Properties prop = new Properties();
prop.load(input);
System.out.println("App Name: " + prop.getProperty("app.name"));
Accessing a resource file from within a JAR.
4. Digital Signatures (Optional)
For security and integrity, JAR files can be digitally signed. When a JAR is signed, the META-INF
directory will contain additional files, typically with .SF
(signature file) and .DSA
or .RSA
(signature block file) extensions. These files allow the JVM to verify the authenticity of the JAR's contents and ensure that it hasn't been tampered with since it was signed.
Creating and Inspecting JAR Files
The Java Development Kit (JDK) provides the jar
utility for creating, viewing, and extracting JAR files. It's a command-line tool that simplifies working with these archives.
1. Create a JAR file
Use the jar cf
command to create a new JAR file from compiled classes and resources. The c
flag means 'create', and f
means 'specify filename'.
2. View JAR contents
Use jar tvf
to list the table of contents of a JAR file. The t
flag means 'list table of contents', v
for 'verbose output', and f
for 'specify filename'.
3. Extract JAR contents
Use jar xf
to extract all files from a JAR archive into the current directory. The x
flag means 'extract', and f
means 'specify filename'.
Create JAR
jar cf myapp.jar com/example/*.class config.properties images/
View JAR
jar tvf myapp.jar
Extract JAR
jar xf myapp.jar