Session 2

An Introduction to the Linux Command-Line

Computer Science II
Object-Oriented Programming


Today's session is a hands-on lab exercise designed to get you started using the Linux command line to manage files and program in Java. Be sure to ask questions as you have them!

Log In

Once you are seated at one of the computers in 339 Wright, you are ready to log in. If you see a Windows login screen, press ctrl-alt-del, then Shut Down, and finally OK to restart the system. Within a minute or so, the system will re-boot and give you a Linux login screen.

Enter your user name and password. Of course, passwords are not echoed back to the screen, so that anyone who might be looking at your screen cannot see it.

In a few seconds, the system will bring up a graphical desktop that looks a bit like a Windows or Mac desktop, with a couple of icons on the desktop and a palette of icons along the bottom of the screen.

Connect to Exercise Web Page

Click on the icon at the bottom of the desktop that has a picture of the earth inside what looks like a toothed washer. This will open a window for the Linux web browser Konqueror. It will look a little different from other web browsers you've used, but for the most part it will work the way you expect.

In the URL text box, enter this URL:

You should bookmark this page, add it to your home page, or do something to save yourself having to keep typing the URL all semester long...

Choose Course Materials, and then click on the Summary link for Session 2. It will take you to the web page for today's exercise -- beginning with these instructions!

Continue with the instructions you on the web page.

Download JUnit

Click on this link to download the JUnit package we used for tests in Session 1:

Choose Save to disk and then OK.

This will download the from the course web site into your home directory.

Open a Terminal Shell

Click on the icon at the bottom of the desktop that contains a picture of a computer terminal and a seashell. This will open a terminal shell, at which you can enter Linux commands.

You will do most of the rest of the exercise from the command line, even when there is a way to do the same thing through the graphical user interface. (You know what they say: Practice, practice, practice!)

Today, you will create only one terminal shell to do all of the work that follows. In later sessions, you will want to create several shells for use at one time, say, one for text editing, one for compiling programs, one for running the mail program, and so on. Just resize and reposition the windows any way you like.

Inside the shell that you have created, you will see a prompt that looks something like this:


The prompt lists the name of the particular machine you are on and your current directory. The prompt indicates that you can type and execute Unix commands. In order to use the shell, you must make the window active by clicking your mouse on it.

To execute a Unix command, type it to the right of the prompt. When the command is finished running and displaying its output, you will see the prompt again, and you can then type another command. If you make a mistake while typing a command, use the delete key to back up, and re-type the command properly.

Enter your first Unix command now:


This displays a list of the users currently logged into the machine you are using. Don't be surprised if you are the only one!

Unix's shell interpreters are case-sensitive. Be sure your caps lock key is off! All Unix commands are lowercase, though many of our file names will contain uppercase letters.

Prepare JUnit for Use

Verify that you are in your home directory and that it contains the JUnit file you downloaded above. Use these commands:

               ls -l

Unzip the JUnit zip file. Use these commands:

               ls -l
               ls -l

Make a directory for the Junit class archive. Use these commands:

               mkdir java-classes
               mv junit.jar java-classes
               ls -l

Try typing part of a filename at the command line and then hit the tab key. The shell will auto-complete the filename for you.

Verify the move. Use these commands:

               cd java-classes
               ls -l

Add JUnit to Your Classpath

In order to use JUnit when compiling and running Java programs, the Java command will need to know where to find JUnit. We will tell all java-related programs where to find JUnit by adding it to your 'classpath'.

First, check for your shell configuration files. Use these commands:

               ls -al | more
               more .bash_profile
               more .bashrc

The shell program you are using is called bash. These files tell bash how to work for you. They are given as the default files for every new account.

Now, add two lines to the .bashrc file to define your Java classpath. Use this command to open the file in the vi editor:

               vi .bashrc

Use your cursor tomove down to the last line of the file. Type o to create a blank line after the current line. Type the following:

               export CLASSPATH

Hit esc. Type :wq. These commands (1) return vi to command mode and (2) save the file and quit the editor.

Verify the change to the file and then apply it:

               more .bashrc
               source .bashrc

The line we added to your .bashrc tells all Java tools to look for classes first in the directory ~/java-classes/junit.jar and then in the current working directory. The tilde, ~, is Unix shorthand for your home directory.

Connect to Another Computer

Connect to the cowboy server. cowboy is the main student server in the CNS system. Use these commands:

               ls -al
               more .bashrc

Notice that your home directory is available from cowboy and any other machine in its cluster, including the ones in 339 Wright and 110-112 Wright.

Now, return to your local machine. Type ctrl-d.

Download Session 1's Java Program

Now let's see if you can take some small steps without detailed instructions for each Unix command... Ask if you get stuck!

Return to your web browser. Back up to course sessions page. Download the .zip file for Session 1.

Return to your shell. Make sure you are in your home directory. Unzip the Session 1 .zip file.

Use this command to rename the session01 directory directory:

               mv session01 session02

Make a new directory called cs062, and move the session02 directory into it.

Switch into the cs062/session02/bowling-game directory. List its files.

Compile and Run Session 1's Java Program

There are several Java-related Unix commands we can use. The first two we need are javac, which compiles a program, and java, which runs a program.

Compile and run the Session 1 program. Use these commands:

               ls -al
               java BowlingGameTest

Note: You must type the .java extension when compiling a class, and you must not type the .class extension when running a program.

You just ran the BowlingGameTest as the main program, which uses the text interface to JUnit. Now run the graphical interface using this command:

               java junit.awtui.TestRunner BowlingGameTest

Notice that the JUnit window comes up but that you can no longer user your terminal shell. Do you remember how to run JUnit "in the background"?

Make your shell window active. Hit ctrl-c. This terminates JUnit. Hit the up arrow and add a & to your java command. This runs the junit.awtui.TestRunner in the background, leaving your shell free for more work.

Extend the BowlingGame Test Program

Now let's see if you can take some bigger steps without detailed instructions for each step... Again, ask if you get stuck!

Use the vi editor to add a new test to Make it different than the other tests, with a mix of strikes, spares, and open frames. You can use the Y and P commands ('yank' and 'put', respectively) to copy and past a line of text.

Compile the program, and re-run the tests from the JUnit window.

If your test fails, figure out why. (The error will be in your new test method!) Fix it and run again.

Use this command to delete all the class files from your directory:

               rm *.class

Be sure that "*.class" contains no spaces!

List the files in your directory to see what all remains.

Log Out

Click on the icon in the lower lefthand corner of the window, the one with a big K and that same toothed washer. Choose Logout and then OK to logout of the system.

Wrap Up

Eugene Wallingford ==== ==== January 13, 2005