Updating Your JAR Files
Once you create a JAR file, you can add to it using the JAR tool. This is done using the u option which is showcased with the syntax below.
jar uf jar-filename.jar [space delimited list of files to be added]
For example, we might have:
jar uf jar-filename.jar file1.txt subdir3/file2.txt
In our syntax, we use the u option to indicate that we are performing an update of our existing JAR, jar-filename.jar. The f option, as before, signifies that we shall indicate our filename via the command line (i.e., jar-filename.jar)
We also specify a list of the files that we want added to our JAR file. Any files that already exist in our archive will be overwritten if there is a conflict of a same pathname.
So what was that MANIFEST.MF file we saw earlier? The JAR embeds a manifest, containing information about the files which constitute the JAR file. There is only one manifest file per JAR file and it is always housed in the META-INF directory. This information might include meta information about electronic signing, version control, package sealing, and a variety of other information.
Below, you can see the contents of a sample manifest file.
Manifest-Version: 1.0 Created-By: 1.4.2_03 (Sun Microsystems Inc.)
As you can see, manifest entries take the form of name-value pairs, using the form: "headername: value".
The manifest file does not really play a role for basic JAR functionality. It's a lot more important when we start to deal with more advanced JAR functionality, such as signing and authenticating JAR files. Now that you have a base foundation of knowledge of the JAR format, let's address some of that advanced functionality now.
Executing JARs: Specifying the Application Entry Point using the JAR File Manifest
You can execute a Java class directly from a JAR file. This is done using the syntax:
java -jar ExecutableJarFileName.jar
Before we do this, we need to do some work. Create a file named manifest that has the following line in it:
In short, you are defining the Class of your JAR and specifying its fully qualified pathname.
Next create the executable JAR file using the JAR tool using the syntax:
jar cmf manifest ExecutableJarFileName.jar
In the syntax above, we used the c (create), f (file will be specified on the command line) and m (update manifest) options. Using these options, the manifest is updated with the manifest header we created from the file named manifest. After doing this, the JAR file can be executed using the java jar command prefix which was shown earlier.
An important point to take note about executing from a JAR: the auxiliary JARs used by the executed JAR must be referenced through the Class-Path header of the manifest file. In a jar type execution, the environment variable CLASSPATH and command line class paths are ignored by the JVM. You can learn more about this in the Extensions section of this article.