Skip to content

Commit 7c48328

Browse files
committed
docs(JAR): explain what a JAR file is with examples
What - Documented **JAR (Java ARchive)** files as packaged archives for Java applications and libraries. - Key features: - File extension `.jar`. - Bundles multiple `.class` files, resources (images, configs), and metadata into a single archive. - Built on ZIP format with a special `META-INF/MANIFEST.MF` file. - Used for distribution, deployment, and execution of Java programs. - Can be executable (with a Main-Class entry) or non-executable (used as a library). Why - Simplifies distribution by combining many class files/resources into one file. - Enables reusability of code as libraries (e.g., external dependencies). - Supports portability across platforms with the JVM. - Provides versioning and metadata for deployment tools. How - **Creating a JAR file:** 1. Compile Java program: ``` javac Main.java ``` 2. Package into JAR: ``` jar cfe MyApp.jar Main Main.class ``` - `c` → create archive. - `f` → output to file. - `e` → specify entry point (Main-Class). java -jar MyApp.jar jar tf MyApp.jar Lists files inside archive. Examples 1. **Hello World executable JAR** - `Main.java`: ```java public class Main { public static void main(String[] args) { System.out.println("Hello, World from JAR!"); } } ``` - Compile and package: ``` javac Main.java jar cfe Hello.jar Main Main.class java -jar Hello.jar ``` - Output: ``` Hello, World from JAR! ``` 2. **Library JAR** - Create utility classes, bundle into `Utils.jar`. - Add to another project’s classpath: ``` java -cp .;Utils.jar MyApp ``` Logic - Inputs: compiled `.class` files and resources. - Outputs: compressed `.jar` archive. - Flow: 1. Java compiler generates `.class`. 2. jar tool packages classes/resources into `.jar`. 3. JVM runs `java -jar` if Main-Class is defined. - Edge cases: - Without Main-Class in manifest, `java -jar` fails. - Paths with spaces must be quoted. - Complexity / performance: - Simple ZIP compression; overhead minimal. - Concurrency / thread-safety: - Multiple JARs can be used simultaneously on the classpath. Real-life applications - Delivering Java applications as single executable files. - Packaging reusable libraries (e.g., `gson.jar`, `mysql-connector.jar`). - Used in build tools (Maven, Gradle) for dependency management. - Required in Java EE/ Jakarta EE deployments (WAR/EAR contain JARs internally). Notes - Executable JAR requires `META-INF/MANIFEST.MF` with Main-Class specified. - Non-executable JARs serve as libraries only, added to classpath. - JARs can be signed for integrity and security. - Still widely used even with newer formats (like JMOD in Java 9+).
1 parent a032315 commit 7c48328

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

Tips And Tricks/Jar File Java.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ metadata into a single file.
66
A JAR makes it easy to distribute Java libraries, applications, and resources as one portable unit.
77

88
Key characteristics:
9-
• ZIP-based archive (.jar).
10-
• Contains compiled .class files, resource files (properties, images), and an optional META-INF/MANIFEST.MF.
11-
• Can be executed (if it contains a Main-Class entry in the manifest) or used as a library on the classpath.
9+
• ZIP-based archive (.jar).
10+
• Contains compiled .class files, resource files (properties, images), and an optional META-INF/MANIFEST.MF.
11+
• Can be executed (if it contains a Main-Class entry in the manifest) or used as a library on the classpath.
1212

1313
Why JAR files are important
14-
• Distribution — package everything needed (classes + resources) into one file for easy sharing and deployment.
15-
• Reproducibility — stable artifact that can be versioned and archived.
16-
• Classpath management — simplifies dependency handling (one file instead of many loose class files).
17-
• Security & Integrity — supports signing (JAR signing) and manifest metadata.
18-
• Performance — fewer filesystem entries and easier I/O handling; commonly used by class loaders.
19-
• Tooling compatibility — all build tools (Maven, Gradle, Ant), application servers, and the JVM understand JARs.
14+
• Distribution — package everything needed (classes + resources) into one file for easy sharing and deployment.
15+
• Reproducibility — stable artifact that can be versioned and archived.
16+
• Classpath management — simplifies dependency handling (one file instead of many loose class files).
17+
• Security & Integrity — supports signing (JAR signing) and manifest metadata.
18+
• Performance — fewer filesystem entries and easier I/O handling; commonly used by class loaders.
19+
• Tooling compatibility — all build tools (Maven, Gradle, Ant), application servers, and the JVM understand JARs.
2020

2121
Basic JAR layout
2222

0 commit comments

Comments
 (0)