19 Mar 2012 [ 185 week10 hw ]

Jar files are the standard way of packaging Java libraries and programs.

The classpath is the set of directories (or jar files) where Java looks for libraries and programs to load, similar to the PATH environment variable for a shell. The notation used is often similar -- when you specify the classpath on the command line, you list directories separated by ; (on Windows, or : on other systems), just like PATH.

Setting the classpath

Both java and javac need to know where to look for any libraries that your program needs to use. The JDK setup should automatically configure those programs to find all the builtin Java libraries, but not any additional libraries you might download.

To add directories to the list to search, you can specify an extra -cp flag each time you run java or javac, which looks like this on Windows:

java -cp ".;other\directory;somejarfile.jar" MyClass

Note that . indicates the current directory, which you probably want to include to make sure that you can run programs in your current directory.

Alternately, you can set a CLASSPATH environment variable to add directories:

set CLASSPATH=.;other\directory;somejarfile.jar
java MyClass

To add a jar file for use in NetBeans, right-click on the libraries section of your project and choose "Addd JAR/Folder":

NetBeans lib addition

Jar files

Jar files are just zip files with a set of conventions for what the Java runtime or compiler expect to find inside. The JDK comes with a jar program that you can use to create jar files on the command line.

The basic structure inside a jar file is just a file that contains information about what else is in the jar file called a manifest, plus all the other files. Often, all the other files will just be java class files. Here's the contents of the jar file we created last time, which can be listed using the command jar tf Hello.jar:


We used ant to create the jar file, but we could use the jar program to build it instead. Running from within the build directory, use the following command:

jar cfe Hello.jar HelloGUI *.class

The first argument is always an acronym for the operation to perform. Here, cfe stands for create file with entrypoint, and then we specify the output file, the entrypoint (the class that we run as the main class), and all the files to package inside Hello.jar.


Create a Java program that uses the Processing library. core.jar contains the library that you'll need to use to compile and run your program; the reference and tutorial sections will also be helpful -- take a look to see what commands and variables are available for setting colors, drawing, and interacting with the mouse and keyboard. Make sure to set your classpath to make core.jar accessible, and test out your final program.

Here's a short example:

import processing.core.*;

// Uses the Processing library to animate a simple drawing.
// The Processing library provides a framework for creating
// interactive drawings or animations.
public class ProcessingExample extends PApplet {

    // size of circle to draw
    private float size = 20.0f;

    // Called once for initial setup
    public void setup() {
        // window size
        size(500, 500); 
        // set framerate (in frames per second)
        // use smooth edges when drawing
        // initial background color

    // Called once per frame
    public void draw() {
        // new background to clear old frame
        fill(100, 100, 100, 20);
        rect(0, 0, width, height);
        // If mouse is down, draw a circle
        if (mousePressed) {
            ellipse(mouseX, mouseY, size, size);
            // Pick new random by adding or subtracting a
            // random amount
            size = size + random(-5.0f, 5.0f);
            // constrain size to be with interval [5, 80]
            size = constrain(size, 5, 80);

    public static void main(String[] args) {
        PApplet.main(new String[] {"ProcessingExample"});

To submit, either do your work in your 185-hw repository in a folder called hw8 (and post to your BitBucket account when you're done), or email your answers in a zip file to jal2016@email.vccs.edu with subject CSC 185 HW8.

Due Tuesday, Mar 27.


This jar file tutorial covers many of the details (what we used plus a lot more) of working with jar files and the jar program.