CMPSCI 383: Artificial Intelligence

Fall 2014 (archived)

Assignment 06 Parsing Example

I’ve written a short example showing how to use Google’s GSON library to parse JSON, as you might do for Assignment 06. You can download the code here: RejectionSampler.tar.gz.

Several people asked for guidance in adding GSON to the classpath, either in Eclipse or on the command line. If you’re interested, read on. Know that some of what I’ve written below is a simplification, and that you should someday read more elsewhere so that you fully understand Java’s class loader semantics.

What is the classpath?

Often, when you tell a compiler (javac Foo.java) to convert your source (.java) files into bytecode (.class) files, and when you tell the Java virtual machine to execute that bytecode (java Foo), your code isn’t standalone. It might depend upon part of the Java Class Library. For example, System is actually java.lang.System, or you might import java.util.ArrayList or the like. How do the compiler and VM know where these files are?

By default, they look in three places. An install of the JDK or JRE places the bootstrap classes, that is the Java Class Library and some other stuff, on your machine. This is always searched when looking for classes. There’s also a system-wide extension directory where an administrator might install additional classes. Finally, your classpath is searched.

The classpath is, by default, the current working directory of the compiler or JVM. Suppose class Foo depends upon class Bar. When you type javac Foo.java in the directory containing Foo.java, javac searches the bootstrap and extension directories first (where, of course, there is no Bar class), then it searches your classpath. Since you haven’t set it, it uses the default value, the current working directory. If Bar.java is in the current directory, it compiles and uses it.

Setting the classpath

Sometimes you want to add other paths to the classpath, or you want to add a .jar to the classpath. To do so, you manually specify it. There’s two ways to do so. One is to set an environment variable named CLASSPATH. Another is to pass an argument to the javac and java commands themselves. This argument is -classpath, and it’s followed by a list of paths to be searched.

When specifying this list, there’s two things to keep in mind. First, once you specify a classpath, the default is no longer used. So if you want to continue searching the current directory as part of the classpath (and you almost certainly do), then you need to remember to add it to the manually specified classpath yourself. The shorthand for “current working directory” in most OSes is . (a single dot).

Second, you need to specify the list in an OS-dependent way. On Windows-based systems, you use \ to separate directories, and ; to separate paths; on Unix-based (including OS X and the Edlab Linux machines), you use / to separate directories and : to separate paths.

A command-line example

To compile and execute the sample code I’ve provided successfully, you need to include the GSON jar on your classpath. You could change directory into the src directory, then, on Windows, you’d use:

1
2
javac -cp .;..\lib\gson-2.3.jar RejectionSampler.java
java -cp .;..\lib\gson-2.3.jar RejectionSampler ..\wetgrass.json

On a Unix system, you’d use:

1
2
javac -cp .:../lib/gson-2.3.jar RejectionSampler.java
java -cp .:../lib/gson-2.3.jar RejectionSampler ../wetgrass.json

Note that .. means “the parent of the current directory.”

The command lines above tell both the compiler and the VM to search both the current working directory, and within the .jar file – they both know to “look inside” .jars for the .class files contained within.

Adding JARs to the classpath in Eclipse

When you first import the files in the archive above, you’ll see something like this:

Note that the import statement shows an error. To fix this, you need to access the properties of the RejectionSampler project. Either left click on it once in the Package Explorer to highlight it, then either select Properties in the File menu, or right-click on it in the Package explorer and select Properties.

Select the Java Build Path option in the list at left, then select the Libraries tab. It should look like this:

Since the jar we want is already in the project (in /lib), click the Add JARs… button. In the window that pops up, select the gson-2.3.jar file as shown here:

Eclipse should then be able to resolve the error and build the project. Further, when you set up a Run Configuration to execute the program from within Eclipse, it will automatically add the jar to the classpath.