Commit 7c48328
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
1 file changed
+9
-9
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | | - | |
11 | | - | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| |||
0 commit comments