-
Notifications
You must be signed in to change notification settings - Fork 4
Working With Jar Files
JAR files allow Java programmers to package all of their class files and resources into a single file with the .jar file extension.
A JAR file is a Java ARchive that can store any combination of
- class files
- source files
- other resources (images, sounds, data, etc)
The jar program is part of the JDK and the basic usage looks something like the following
jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
In general, you call the jar command with any number of specified options, then you name the name of your jar file, followed by the files you would like in your jar. We'll discuss some of the most common options below.
| Option | Description |
|---|---|
| v | Use verbose output |
| c | Create a jar archive |
| t | List contents of a jar archive |
| x | Extract a jar archive |
| f | Specify archive file name |
| m | Include a manifest file |
| e | Specify an application entry point |
| C | Change to specified directory and include the following file |
Assume for all examples that the current working directory contains Foo.java and Bar.java.
jar cf Assignment00.jar Foo.java Bar.java
or to include all .java files...
jar cf Assignment00.jar *.java
or to include entire directories...
jar cf Assignment00.jar /src
jar tf Assignment00.jar
jar xf Assignment00.jar
Note: This is not required for submitting assignments, but may be relevant in future assignments. Besides, executable jar files are cool. You can take any number of .java and .class files and put them in a single jar to distribute as a complete program.
If a jar file is executable, it's possible to run the Java program from within the jar without having to extract the jar file itself. This only works under two conditions, first the .class files must be included in the jar, and second, either an entry point or a manifest which contains the class with the main method must be present.
Executable jar files can be ran in the following way:
java -jar JarFileName.jar
An entry point points to the class that contains the main method. For the rest of these examples, let's assume that Foo.java contains the main method for our project.
jar cfe Assignment00.jar Foo Foo.java Bar.java Foo.class Bar.class
Or this might be shortened by using wildcards
jar cfe Assignment00.jar Foo *.java *.class
Take note that the first occurrence of Foo in the above examples is the argument telling jar which class the main method resides in. Jar simply needs the class name, not the extension of the file.
This method is quite easy and unless you're doing anything more complicated, I would recommend this method over the manifest method presented next.
Create a file called Manifest.txt with the following contents.
Main-Class: ClassWithMain
Where ClassWithMain is replaced by the name of class that contains your main method. Also note that the last line of the manifest file must be a blank line.
A manifest file for our example might look like the following:
Main-Class: Foo
Then, when making the jar, be sure to include the manifest file.
jar cfm Assignment00.jar Manifest.txt *.java *.class