JAR
JAR (Java Archive) is a package file format used to aggregate multiple Java class files, associated metadata, and resources (text, images, etc.) into one file for distribution.
What is a JAR file?
Section titled “What is a JAR file?”A JAR file is essentially a ZIP file with a .jar extension that contains compiled Java classes and resources needed for a Java application or library.
Creating JAR files
Section titled “Creating JAR files”The basic syntax for creating a JAR file using the command line:
jar cf jar-file input-filesWhere:
cindicates you want to create a new archivefspecifies the archive filenamejar-fileis the name you want to give your JAR fileinput-filesare the files you want to include
Example:
jar cf myProgram.jar *.classThe Manifest file
Section titled “The Manifest file”JAR files can include a special META-INF/MANIFEST.MF file that contains metadata about the JAR:
Manifest-Version: 1.0Main-Class: com.example.MainClassTo create a JAR with a manifest:
jar cfm myProgram.jar manifest.txt *.classExecutable JAR files
Section titled “Executable JAR files”To create an executable JAR:
- Create a manifest file with Main-Class specified
- Include the manifest when creating the JAR
Example manifest (manifest.txt):
Main-Class: com.example.MainClassCreating the executable JAR:
jar cfm myProgram.jar manifest.txt *.classRunning the JAR:
java -jar myProgram.jarViewing JAR Contents
Section titled “Viewing JAR Contents”To list the contents of a JAR file:
jar tf myProgram.jarExtracting JAR Contents
Section titled “Extracting JAR Contents”To extract the contents of a JAR file:
jar xf myProgram.jar