Compiling Java Programs on your student.cs UNIX Account

Although the online test-compile facility will compile your assignments in UNIX for you, you may want to log into your UNIX account and compile your assignments yourself. This page provides step-by-step instructions on how to do this.

These instructions will only work on your student.cs UNIX account. These instructions will not work if you are trying to compile assignments on your UNIX/Linux/FreeBSD machine at home.

Unfortunately, not all of the UNIX servers have the Java compiler installed. However, Java is installed on all of the following servers (as of January, 2001):

If you connect to any of the above servers, you should be able to compile your Java programs.

Setup

Before compiling your programs, you must configure your UNIX account so that it can find the CS 134 libraries:

  1. Connect to one of the servers listed above. For example, if you are using Windows 95/98, select Start->Run, then type
      telnet rees.math.uwaterloo.ca
  2. Log in. You should see some messages, and then get a prompt that looks something like:
      @rees[10]%
  3. The first thing to do is make a backup of your configuration file. At the prompt, type
      cp .cshrc cshrc.bak
  4. Next, we want to edit the configuration file to include the new information. For simplicity, we will use the PICO editor to change the configuration file. Type
      pico .cshrc
  5. Use the arrow keys to move down the file until you see the lines
            ### Set environment variables that are only used by interactive
            ### programs here (e.g. RNINIT, ORGANIZATION).
      
  6. Under these lines, insert the following code to let UNIX know where to find the libraries:
    # set  class path 
    setenv JAVA_HOME "/fsys2/.software/arch/javajdk-1.2.1/distribution/lib"
    setenv CLASSPATH "${JAVA_HOME}/classes.zip:\
         :/u/cs130/cifs_exports/JAVA/library.zip:\
         :/u/cs134/cifs_exports/structure.zip:\ 
         :/u/cs134/cifs_exports/cs134.zip"
    
    You may find it easiest to copy and paste the above code into your file. Otherwise, be very careful to type the code exactly. Spacing is very important! If lines are split when you copy and paste, you must join them back together (in PICO, try moving the cursor to the beginning of the second fragment and pressing the backspace key until the line is whole again, then add a space if one is missing).
  7. Finally, save the changes. Press
      <ctrl>-X
    (<ctrl> and "x" at the same time) and type "y" to save the changes, then <enter> to save the file under the same name (".cshrc").
  8. PICO should now exit, and you should get your prompt again. To make the changes active, log out, then reconnect to the UNIX machine and log back in.
  9. To test that you have not messed up your account, we will list your account contents. At the prompt, type
      ls -al
    You should see a listing of files. If you get the message   ls: Command not found
    then you have messed up. To fix this, type
       /.software/local/.admin/bins/bin/cp cshrc.bak .cshrc
    to retrieve your backed-up configuration, then
      source .cshrc
    to restore your account. You can then start this configuration again until you get it right (or you can give up).
  10. If the ls command works, then you should be ready to compile and run Java programs on your account.
You only need to configure your account once.

Compiling and Running Programs

In order to compile your assignments, they must be on the UNIX server. If you saved your files to the P:\ drive of your Polaris account, the files are already on the server. We'll assume that you saved your .java files in the directory
P:\cs134\myassignments\

To compile your program, you need to do the following:

  1. Connect to one of the UNIX servers listed above, and log in.
  2. Move to the directory containing your files. For the above example, you would type
      cd ~/cs134/myassignments
    Note that the forward slashes are used to separate directories in UNIX.
  3. Type
      ls
    to list the directory contents. You should see your .java files. If you don't, try the previous step again.
  4. To compile all the files in the directory, type
      javac *.java
    If your program has errors, the compiler will list them. Otherwise, you will get a prompt.
  5. Run your program by typing
      java classname
    where classname is the name of the class containing your public static void main(String args[]) method. For example, if the main method was in a class called Simulation.java, you would type
      java Simulation
    Note that you do not include the .class at the end of the file when running Java programs.