Jar File 101
JAR (Java ARchive) files are a convenient way to package and distribute Java applications, bundling everything your app needs—class files, resources, libraries, and metadata—into one compressed file. Think of it as a zipped suitcase for your Java project.
What is a JAR File?
A JAR file is a compressed archive containing:
- Class files
- Resources (images, config files, etc)
- Manifest file (metadata, including the Main-Class for executable JARs)
Creating a JAR File
- Compile your Java files:
javac com/example/*.java
- Create the JAR:
jar cf myapp.jar com/example/*.class
- Add a manifest (for executable JARs):
jar cfm myapp.jar MANIFEST.MF com/example/*.class
- Run your JAR:
java -jar myapp.jar
Why use JAR files?
- Convenience: Everything in one file.
- Portability: Works anywhere with a JVM.
- Modularization: Break large apps into multiple JARs.
- Library Distribution: Many Java libraries are distributed as JAR files.