2. JAR Files
• JAR (Java ARchive) is a package file format typically used to aggregate many Java class files and
associated metadata and resources (text, images and so on) into one file to distribute application software
or libraries on the Java platform.
• JAR files are fundamental archive files, built on the ZIP file format and have the .jar file extension and does
not have to be compressed. A JAR file allows Java runtime to efficiently deploy a set of classes and their
associated resources. The jar command is a utility that comes with the JDK.
Creating JAR file
• jar cf jar-file.jar input-files-separated-with-space
• The c option indicates that you wish to create a jar, and
• The f option indicates that output should be sent to a file,
• jar-filename is the name of the resulting Jar file and
• input-files are the files you wish to include.
View contents of JAR file
• jar tf jar-filename.jar
• The t option is to print a table of contents and
• the f option indicates that your are reading from a file,
• the jar-filename is the filename of the jar you wish to use.
Instructor: Tanzila Kehkashan
2
3. Manifest Files
• If an application is bundled in a JAR file, the Java Virtual Machine needs to be told what the entry
point to the application is.
• An entry point is any class with a public static void main(String[] args) method. This information is
provided in the Main-Class header. We first create a text file named Manifest.txt with the following
contents:
• Main-Class: PackageName.ClassName
• Here Package.ClassName.main() will be executed.
• Warning: The text file must end with a new line or carriage return. The last line will not be parsed
properly if it does not end with a new line or carriage return.
• We then create a JAR file named MyJar.jar by entering the following command:
• jar cfm MyJar.jar Manifest.txt MyPackage/*.class OR
• jar cmf Manifest.txt MyJar.jar MyPackage/*.class
Instructor: Tanzila Kehkashan
3
4. Extracting Files from JAR File
• If we wish to extract a file from the Jar file, we use the following command.
• jar xf jar-filename archived-files
• The x option indicates we wish to extract files,
• the f option indicates from a jar file,
• the jar-filename is the filename of the jar file to extract from and
• the archived-files are the files to extract.
• In our example we can do the following:
• jar xf project.jar MyClass.class images/logo.gif
• This will produce no output on the command line, but will extract the class file and the image file.
• We can also extract all files by simply not specifying which files to extract.
Instructor: Tanzila Kehkashan
4
5. Run JAR File
• When you run the JAR file with the following command, the main method of class executes:
• java -jar JarFileName.jar
• Or simply type in command prompt:
• jar FileName.jar
• Or just double click on jar file using MS-Windows interface.
Common Mistakes
• Forgetting to leave a blank line after the main class declaration in the manifest.txt. OS may return “Fail
to load Main-Class manifest attribute” error.
• Failing to compress all your files. You have to type in all the files you want to compress. Separate
these files with a space.
Instructor: Tanzila Kehkashan
5