Jar files are much like ZIP files. The ZIP file format is used to compress a number of files into 1 file (an archive). This is useful so that one need not worry about transfering or copying a number of .class or .java files but rather create 1 archive with all of the files contained (and compressed) into it.
jar cf
archive_file_name.jar
file_to_be_put_in_archive1
file_to_be_put_in_archive2
file_to_be_put_in_archive3
jar
cf
lab7.jar
StackV.java
StackFrame.java
StackL.java
Creates an archive file named lab7.jar that contains
each of the files StackV.java, StackFrame.java, and StackL.java.
jar is the command to run the jar utility.
cf are the 2 options to use with the jar utility.
c is the option to create a new archive with the archive_name.jar (given) and f is the file name specified.
-c create new archive
-t list table of contents for archive
-x extract named (or all) files from archive
-u update existing archive
-v generate verbose output on standard output
-f specify archive file name
-m include manifest information from specified manifest file
-0 store only; use no ZIP compression
-M do not create a manifest file for the entries
-i generate index information for the specified jar files
-C change to the specified directory and include the following file\
Multiple options can be used together. They all must appear after the "jar" command with no white space separating them.
Assume there is a Java application consisting of three source files that need to be distributed:
File1.java
File2.java
File3.java
We will name the JAR file test.jar.
To make a JAR file with just File1.java:
jar cf
test.jar
File1.java
To make a jar file named test.jar with all three files listed separately:
jar cf
test.jar
File1.java
File2.java
File3.java
To make a file with all java files using a pattern match:
jar cf
test.jar *.java
This assumes that the source files are in the same directory
you are running the jar command in and they are the only .java files
in the directory (the * will put ALL files ending in .java in the jar).
All JAR files contain something called a manifest file which holds information Java wants to know. One piece of information a manifest file may contain is the name of a class that will be run if the JAR file is executed.
The first thing you must do is create a text file that lists the "main" class - the class that has the main method you want executed when the JAR is executed. For example, File3 from the above example has the main method we want to execute. Create a text file called "mainClass.txt" with the following text:
Main-Class: File3
IMPORTANT: the text file only needs the one line of text for this purpose. However, the file must end with a blank line or this will not work, ie the file has two lines in it - the second one is empty. Note too the class is called "File3" and NOT "File3.java" (the file containing the source code) or "File3.class" (the file containing the byte codes). If your class file is in a package hierarchy, you must use the fully qualified name of the class (eg "myPackage.MyClass").
Now run the jar utility with this command line:
jar cmf mainClass.txt example.jar *.classWith this line, jar will create a JAR file (option c) with modifications to the manifest file (option m) as specified within mainClass.txt, naming the JAR file (option f) as example.jar and including everything that matches the pattern *.class
To run an executable JAR from the command line:
java -jar example.jar