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.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.
You can also extract the contents of a Jar file with any program that extracts .zip files.
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.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.
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.
© 2014-2022, Old Dominion Univ.