Jar Files

Overview

A JAR file is basically a special ZIP file. JARs are commonly used to bundle Java classes together into a single file that you can then easily transfer, execute, or digitally sign. Since the jar tool comes with the JDK, everyone in 211 will have access to it regardless of platform. So we'll use JAR as a standard format to bundle multiple files together for Tamarin submission.

Bundling Source Code for Tamarin

Open a command prompt and navigate to the directory containing your .java source files. Then type a command something like this:

  jar vcf UsernameA06a.jar Node.java Stack.java PyramidStack.java UsernameA06a.java

jar is the name of the program. The next argument gives 3 options: v, which will produce verbose output, c with creates a new jar, and f, which creates a file rather than printing to standard out. The next argument is the name of the .jar file you want to create. The remaining arguments are the files you want to add to the new jar.

If the current directory contains only the .java files for the current assignment, you can shorten the above to:

  jar vcf UsernameA06a.jar *.java

which will bundle all .java files in the current directory. The verbose output will let you see exactly what got added.

Creating an Executable JAR

If you're submitting to Tamarin, you only need to include your .java files, as described above. If you are creating a jar file for distribution, though, then you would need to include your .class files. Also, you can specify which class should be executed when the jar is executed. For example:

  java vcfe UsernameA06a.jar UsernameA06a *.class *.java

The extra e option means you need to specify the name of the class to run when the jar is executed. If you don't want to include your source code, leave off the *.java.

To execute this jar, you'd use:

  java -jar UsernameA06a.jar

More information

To see a usage message listing all the jar tool options, run jar with no arguments.

For more information about what else you can do with JAR files, see the tutorial.