Lab Exercise #2
for labs meeting Friday, Jan. 24, 2002
Objectives: The purpose of this lab is to give you some practice
in organizing and manipulating your Unix file system, and to get ready
to use the program distribution that the course textbook provides. In working
with Java beyond an introductory course, it is really important to understand
directory structures, because Java compilers require a correspondence
between your package/class structure and your directory structure.
Setting up your directory structure.
There is no special setup script for this course, because it's useful
for you to understand the details of your file system by setting up some
of it yourself. We assume you already have a csci53 directory,
either because you took CSci 53, or because you did the CSci 53 first lab
exercise before attempting this one.
-
First modify your .kshrc file to let the Java compiler know how to find
all yourJava files.
The .kshrc file (short for "Korn Shell startup file") contains
commands that are executed each time you enter a copy of the shell -- when
you log in, when you call jcompile, etc. Move to your top-level
directory, and bring .kshrc into your editor. It should contain
the line . ~csjava/csci53.profile. Immediately below this line,
add the line
export CLASSPATH=~:~/csci53:~/csci133:$CLASSPATH
which extends the path the compiler should take in looking for Java
sources and class files. In Unix, the symbol ~ ("tilde") refers to the
home directory.
-
Now learn some useful variants of the Unix ls command.
Move to your top-level directory, and type each of the following, noting
down a description of what each one displays. Note that these are lower
case L's and not numeral 1's.
ls -a
ls -l
ls -al
ls -alR
ls -lR
The last variant will be very useful in this lab. Each time this handout
says "list your file system", use the last command above.
-
Now copy a "blank" directory structure from the distribution account.
This is in the form of a "zip archive"; it was created with the info-zip
program. (For more info on this program, just type zip.)
To copy the archive, make sure you are in your top-level directory (use
pwd
for this), then type
cp ~csjava/csci133.zip .
which puts a copy in your file system.
-
Now expand the zip archive.
Type
unzip csci133
and then list your file system to see the results. Notice the empty
directories, with empty subdirectories, that are now in your file system.
-
Now set up a "shortcut" to the CSci 133 textbook program library.
Move into your csci133 directory, then type
ln -s ~csjava/programs133
which makes the program library visible to you. In CSci 53, all the
programs were in one directory (except for Keyboard). In this course, the
programs are distributed into subdirectories. To see this, type
ls -R programs133
and note the basic file structure.
-
Now examine one of the program files.
Type
vi programs133/edu/colorado/geometry/Location.java
and note that the first actual code line reads package edu.colorado.geometry;
The subdirectory structure matches the package structure. Java
compilers REQUIRE this! In this course, whenever you copy a file from the
library, you must put it into the equivalent location in your own file
system, otherwise you will surely get compilation errors!
-
Now copy this program into its proper place in your own file system.
Type
cp programs133/edu/colorado/geometry/Location.java edu/colorado/geometry
which gives you your own copy of the source file.
-
Now compile the class file.
Type
jcompile edu/colorado/geometry/Location.java
You should get successful compilation messages. The listing and class
files are in that same subdirectory - try listing that subdirectory to
be sure. Also move down into the subdirectory and back.
-
Now compile an application that uses the class file:
Pretend that Project 2 requires you to modify one of the book's applications.
Make sure you are in your csci133 directory, then make a subdirectory
Project2:
mkdir Project2
Now copy a program from the book, namely programs133/applications/LocationDemonstration.java,
into this new subdirectory. Move into the new subdirectory, and compile
as usual. Note that the listing and class files are here now. Run the program
as usual. If it puts out reasonable-looking output, everything is working
properly.
-
Now modify the application to use something from csci53.
Make sure your csci53 directory still contains a copy of Screen.java.
If not, copy it again from csci53/programs53.) Now modify LocationDemonstration.java
to clear the screen and beep before running. Make sure you include import
csci53.Screen at the top! Compile and run the modified program.
-
Now make sure cs1.Keyboard can still be found.
Modify LocationDemonstration.java to prompt the user for his
or her name before doing anything else. Compile and run. Make sure to include
import
cs1.Keyboard at the top.
-
Congratulate yourself on getting through all this!
(end of lab)