A JAR file is similar to a tar file in that it constitutes an archive of Java class files and source files. JAR files are created with the jar command and the jar command takes many of the same arguments as tar. The jar command automatically performs compression on your class files using the ZIP file format.
You can use the jar command to package all the class files for your application into a single file. If you also tell the jar command the main class to access within the jar file, then you can treat the jar file as a pseudo-object file. Finally, if you want, you can also include your source files in the jar file, although normally you would not do so. In this course we want you to submit both your class and source files, so you will be including the source files in your jar file.
The following table, which I copied from Sun's tutorial website on the jar command, shows the most common uses of the jar command:
Operation | Command |
---|---|
To create a JAR file | jar cf jar-file input-file(s) |
To view the contents of a JAR file | jar tf jar-file |
To extract the contents of a JAR file | jar xf jar-file |
To run an application packaged as a JAR file (requires the Main-class manifest header) | java -jar app.jar |
You can tell java how to find the entry point within your jar file by creating a manifest file. A manifest file can have any name you choose although manifest.txt is a commonly chosen one. A manifest file should contain the following single statement:
Main-Class: classname
Main-Class is a required keyword and classname is the name of the class that contains your application's main method.
Here is an example manifest file for an application whose main class is SalaryList:
Main-Class: SalaryList
If you are using packages, then you need to fully qualify the class name. Here is an example manifest file for an application whose main class is named LexDriver and which is contained in a package named formula:
Main-Class: formula.LexDriver
When you create your jar file, you can include m as a command line option. This option causes java to use the file specified after the m flag as the manifest file. For example, if the above file is named manifest.txt and your files are in a directory named formula, you could create your jar file with the following command:
jar cmf manifest.txt formula.jar formula
Note that you can specify all of your flags before specifying the files. The files should be specified in the same order as the flags. The name of the jar file does not have to be the same as your main class.
The jar tool has a -e flag that allows you to specify the application's entry point without creating a manifest file. For example the command:
jar cfe Salary.jar SalaryList *.classwill automatically create a manifest file that contains SalaryList as the application entry point.
In order to execute a jar file that has an entry point specified, invoke java with the -jar option and the jar file's name. For example:
java -jar formula.jar
Here are some sample commands for creating jar files. All of the example commands assume that the manifest file is named manifest.txt. If you choose to use a different name for the manifest file, then change manifest.txt to whatever file name you choose.
jar cmf manifest.txt foo.jar *.class
jar cmf manifest.txt foo.jar *.class *.java
jar cmf manifest.txt foo.jar foo*.classThis command will scoop up both the foo.class file and any nested classes declared in the foo class.
jar cmf hw1/manifest.txt foo.jar hw1/foo*.class hw1/foo.java
jar cmf silhouette/manifest.txt silhouette.jar silhouette
jar cmf manifest.txt foo.jar foo.class /Users/bvz/cs102/play/Hello.classthen your jar file won't work properly, because Hello.class will get included in your jar file as /Users/bvz/cs102/play/Hello.class. If instead you try to type:
jar cmf manifest.txt foo.jar foo.class /Users/bvz/cs102/play/then the play directory will be represented in your jar file as /Users/bvz/cs102/play, rather than as play and java will not be able to locate the Hello.class file. Instead you should type:
jar cmf manifest.txt foo.jar foo.class -C /Users/bvz/cs102/ play/Hello.classThe -C flag causes the jar command to "cd" to the /Users/bvz/cs102/ directory and then grab the play/Hello.class (it will create a directory for play in your jar file and store Hello.class in the play directory). The jar file will now contain foo.class and play/Hello.class, which is what you want.
You can find more information about jar files and manifest files here.