Packaging Programs in JAR Files: Commentary

Steven Zeil

Last modified: Sep 21, 2016
Contents:

When you compile a Java program, you can wind up with a lot of .class files. In the Lab, you saw that we could use a single .jar file as a transport mechanism for all of the .class files that make up an entire program. This reading discusses the mechanics of creating and using Jar files.

1 Using JAR Files: The Basics

1.1 Creating a JAR File

The tutorial focuses on creating a jar file using the command-line tool. In a later lab, we’ll look at how to do this interactively via the Eclipse development environment.

1.2 Viewing the Contents of a JAR File

You can also view the contents of a Jar file with any program that reads .zip files. For example,

unzip -l whatever.jar

will display the contents of whatever.jar on most Linux machines.

1.3 Extracting the Contents of a JAR File

You can also extract the contents of a Jar file with any program that extracts .zip files.

1.4 Updating a JAR File

1.5 Running JAR-Packaged Software

If a jar file has been created with a manifest that specifies a main class, you can execute that main class by saying

java -jar jar-file

In some (but not all) operating systems, double-clicking on the icon for a jar file will run that same command.

But not all jar files have manifests. Furthermore, some jar files may contain more than one executable Java program, so you sometimes need to specify which main class you want to run. You cna do that like this:

java -cp jar-file main-class-name

The -cp option, which can be added to both java and javac commands, adds a list of directories and/or jar files to the CLASSPATH for that command. The CLASSPATH is the list of places where Java will look for your source code and compiled object code. So this command runs the main function in main-class-name, but looks for that class in jar-file.

2 Working with Manifest Files: The Basics

2.1 Understanding the Default Manifest

2.2 Modifying a Manifest File

2.3 Setting an Application’s Entry Point

2.4 Adding Classes to the JAR File’s Classpath

This section and the remaining ones are fairly specialized and advanced. You can feel free to skip these for now and come back to them later if you find yourself looking to do more advanced deployment of Java code.

2.5 Setting Package Version Information

2.6 Sealing Packages within a JAR File

3 Signing and Verifying JAR Files

Again, this section and the remaining ones are fairly specialized and advanced. You can feel free to skip these for now and come back to them later if you find yourself looking to do more advanced deployment of Java code.

3.1 Understanding Signing and Verification

3.2 Signing JAR Files

3.3 Verifying Signed JAR Files

4 Using JAR-related APIs

4.1 The JarClassLoader Class

4.2 The JarRunner Class