Creating JARs
JARs are just ZIP files with a .jar extension and a special directory structure. Meta information is in a directory called META-INF, with the manifest file /META-INF/MANIFEST.MF being the most important file.
Beside META-INF, you add any file you want to the JAR file. Classes must be put into the usual Java directory structure.
This is an example layout for a JAR file, with a manifest file and two classes "com.jarfiller.example.MainClass" and "com.jarfiller.example.Helper":
Content of jarfiller-example.jar META-INF/ (directory) META-INF/MANIFEST.MF (Manifest file) com/ (directory) com/jarfiller/ (directory) com/jarfiller/example/ (directory) com/jarfiller/example/MainClass.class (Java class) com/jarfiller/example/Helper.class (Java class)
The manifest file (/META-INF/MANIFEST.MF) describes the content of the JAR file. It is not required, but often automatically added by JAR tools.
The following manifest declares only a main class:
Manifest-Version: 1.0 Main-Class: com.jarfiller.example.MainClass
Rules for Manifest Files
- a manifest contains key/value pairs (called attributes), one per line
- attribute syntax: attribute name, directly followed by a colon (':'), a single space and the value
- values may span several lines. Each additional line must start with one space.
- no additional whitespace allowed (more)
- no empty lines allowed in the main section (there can be additional sections, separated by empty lines)
Common Attributes
| Name | Description | Example |
|---|---|---|
| Manifest-Version | File format version of the manifest, always '1.0', required | 1.0 |
| Main-Class | Name of the class containing the main method in executable JARs | my.package.MainClass |
| Class-Path | Space-separated list of relative paths to JAR files and directories for the classpath | lib/util.jar lib/helper.jar drivers/ |
| Sealed | if "true", then packages are sealed. Otherwise "false". | true |
| Created-By | Creator of the JAR; automatically added by the jar tool; not important | 1.6.0_12 (Sun Microsystems Inc.) |
| Implementation-Title | Name of the program/library contained in the JAR | My Super App |
| Implementation-Version | Version of the program/library contained in the JAR | 1.0d |
| Implementation-Vendor | Vendor of the program/library contained in the JAR | Super Soft Inc. |
| Specification-Title | Name of the specification the library implements (more) | Super API |
| Specification-Version | Version of the specification | 1.0 |
| Specification-Vendor | Vendor of the specification | Super Group |
Beside those common attributes, there are many special purpose attributes. For example, if you package a Java Applet or a OSGi Bundle in a JAR, there are specific (and often required) attributes for them. For a simple library or executable JAR they are not needed though.
Example
Manifest-Version: 1.0 Created-By: 1.6.0_12 (Sun Microsystems Inc.) Class-Path: lib/common.jar lib/helpers.jar lib/util-impl.jar stdlib/j2ee.jar stdlib/mail.jar drivers/mysql-jdbc.jar drivers/jag-ext.jar extensions/ Implementation-Title: Jarfiller Test App Implementation-Version: 0.1 Implementation-Vendor: jarfiller.org
The JDK ships with a command line tool called jar to create, view and unpack JAR files. On Windows, use it like this:
C:\Users\tim>set PATH=c:\Program Files\Java\jdk1.6.0_13\bin;%PATH% C:\Users\tim>jar Usage: jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files ... ...
To create a JAR file from your compiled classes, enter:
C:\Users\tim>jar cvf tmp/jarfiller-example.jar -C workspace/JarTest/bin . added manifest adding: com/(in = 0) (out= 0)(stored 0%) adding: com/jarfiller/(in = 0) (out= 0)(stored 0%) adding: com/jarfiller/example/(in = 0) (out= 0)(stored 0%) adding: com/jarfiller/example/Helper.class(in = 287) (out= 212)(deflated 26%) adding: com/jarfiller/example/MainClass.class(in = 569) (out= 351)(deflated 38%)
To specify a manifest file, either put it into the base directory (would be workspace/JarTest/bin/META-INF/MANIFEST.MF), or use the 'm' parameter:
C:\Users\tim>jar cvfm tmp/jarfiller-example.jar workspace/JarTest/manifest/MANIFEST.MF ^ -C workspace/JarTest/bin . added manifest adding: com/(in = 0) (out= 0)(stored 0%) adding: com/jarfiller/(in = 0) (out= 0)(stored 0%) adding: com/jarfiller/example/(in = 0) (out= 0)(stored 0%) adding: com/jarfiller/example/Helper.class(in = 287) (out= 212)(deflated 26%) adding: com/jarfiller/example/Helper.java(in = 56) (out= 58)(deflated -3%) adding: com/jarfiller/example/MainClass.class(in = 569) (out= 351)(deflated 38%) adding: com/jarfiller/example/MainClass.java(in = 147) (out= 127)(deflated 13%)

