COMP 212 Lab 1: Unix, Emacs, the JDK

This tutorial covers the basics of using the main software tools of this and later courses:

These instructions do not necessarily apply to systems other than Owlnet Sun workstations. While you may do your coursework on other computers, software is not guaranteed to behave identically.


UNIX

Do the following, to get some practice for some of the most useful UNIX commands:

  1. register comp212 will make a few changes to your account, including creating a COMP 212 subdirectory. You should keep all your COMP 212 work in this directory. In it, further create a labs/01 subdirectory, will will contain your work for for this tutorial.

  2. man is a program for viewing online manuals about UNIX commands and features. xman is a GUI version of the same program.

    For example, use man ls to read about a file's permissions, and answer the following questions: If you own a file with permissions -rw-r--r--, can your friends read it? Can they write to it? Who can get to your comp212 directory?

    Read about the flags (such as -l, -A, -F, -t) for using the features of ls. What are the permissions for your newly-created personal ~/comp212/ directory?  ("~" is Unix shorthand for your home directory.)

  3. chmod changes the permission modes of files. Use it to make your COMP 212 directory accessible by yourself only.

  4. alias makes abbreviations for common commands. How can you use it to make ll (or something else of your choice) be shorthand for ls -FlAt (or whatever is your favorite combination of flags).

  5. cp copies files. Copy this file, index.html in the directory ~comp212/public_html/01-spring/labs/01/ to your lab directory.

  6. ln creates "links" to files. Make a "symbolic" link (ln -s) to this lab directory in your own lab directory. What's the main reason for using (symbolic) links? What's an annoying result of using symbolic links together with cd?  (That is, make a symbolic link and navigate around using cd.)

  7. rm removes files. Remove the symbolic link and index.html to get them out of the way for the rest of this tutorial.

  8. chsh will change your "shell", i.e., the program in which you enter UNIX commands. By Owlnet default, you use csh, the C shell. We recommend that you use a shell with more features, either tcsh or bash. The latter may be most familiar to Linux users.

    To use chsh, you need the full path of the new shell. Look at the text file /etc/shells for the full paths of all the available shells. Alternatively, use which tcsh to try to find the path for tcsh (or any other UNIX program).

    Go ahead and use chsh. This is a one-time thing -- it will change your shell for all future logins, or at least until the next chsh.

    In your new shell, what key do you press to repeat previous commands? In your new shell, what key do you press for filename completion?

  9. An unfortunate fact of life is that programs hang or crash. In Unix, you can close programs that hang, without having to reboot, using kill. (This is the equivalent to the Close Window Dialog in Windows.) To use this very powerful command, the computer needs to know exactly what it is you want dead. In UNIX, any running program has a PID (process identification) number. You can find out PIDs using the program top or ps. Once you have the PID of the program you want stopped, you just type kill PID with the appopriate PID.

  10. Read about I/O "redirection" and "piping" in the "About Unix Commands" chapter of Owlnet's UNIX handout or in the online manual for your shell (e.g., man tcsh).

    Use > to send the output of cal 1752 to a file. (See man cal to see why the year 1752's calendar is strange).

  11. Do you know how to copy and paste in UNIX? Well, in case you don't, try highlighting some text by using the left mouse button. What you just did is the same as the Copy function in Windoze. Now, go to where you want to paste the text, and click the middle mouse button (on the Sun workstations). It should automatically paste what was highlighted.

If much of this material is new to you, don't worry. This tutorial covers much more than you need to know for the course. Of course, if you don't understand something, it is up to you to come to us for help. For assistance, you can


Emacs

Emacs is an editor program. Actually, that's an understatement in a couple ways. First, it is pretty much the UNIX editor program. (The default Emacs on Owlnet is GNU Emacs, but there is a competing XEmacs.) Sure, there are others, but not many people use them. Second, it is so much more than just an editor. On some systems Emacs' icon was a kitchen sink, and on old systems a running joke was that its name meant "Emacs Makes A Computer Slow". It is an amazingly configurable program with lots of options. E.g., you can read email and newsgroup postings and more using just emacs. This part of the tutorial will just cover a few basic things. If you want to learn more, check out the Owlnet handout on Emacs.

  1. Now, the most confusing thing for emacs newbies is understanding the notation used for commands. If you check out the Emacs reference sheet, you will see commands like

    • C-x C-f
    • C-x C-c
    • M-x
    What this notation means is, respectively,
    • Press Control-x and then press Control-f, which lets you find a file
    • Press Control-x and then press Control-c, which exits Emacs
    • Press Meta-x, where the Meta key is usually the Esc key

    Some other good keyboard commands include:

    • C-x C-s, to save a file
    • C-g, to cancel whatever command you're in the middle of
    • M-c, where the COMP 212 Emacs configuration sets this key as a shortcut to compile your current program.

     

  2. Another cool thing is that for commands, there is also tab-completion. Try opening a file using C-x C-f. If you just type the first couple letters of a file you want and press tab, emacs will try to complete the name of your file. This is good because as computer scientists, we are lazy.

  3. Like any decent editor, Emacs periodically auto-saves your work. If you notice some files that show up in your directory starting with a #, it's an Emacs auto-save file. In the event of a crash or any other situation where you were forced to exit Emacs without saving your work, you can recover the last auto-saved version via M-x recover-file.

    There are a tremendous number of commands that can follow M-x. If you forget exactly the one you want, don't forget the tab completion to help you figure out which one you're looking for. Or you can try M-x apropos to search for appropriate commands.

  4. You can also change the type and size of fonts and much more. Try holding down the Ctrl key and pressing the left mouse button while Emacs has the mouse focus. Now, try holding down Shift and the left button. I'll leave it to you to play around with it.


JDK: Compiling and Running a Java program

Sun Microsystems, the creator of Java, provides us a free software development kit (SDK), commonly referred to as the JDK, which contains among many tools, the Java Virtual Machine (JVM), a compiler to compile Java source code into byte code, the machine language of the JVM, and an interpreter to run the byte code.  The JDK can be downloaded from Sun's website.  There are several versions and flavors of the JDK.  The one installed at Rice is the Java 2 Software Development Kit, Standard Edition version 1.3.0.  We will have it upgraded to version 1.3.1 soon.

A Java program consists of one or more classes, one of which must have a "main" method which serves as the entry point to the program.  The main method of a class has a very special syntax as illustrated in the following example.

public class Greeting {
    public static void main(String[] args) {
        System.out.println("Howdy Pardnuh!");
    }
}

Here are the meanings of the key words and the parameter list that appear in the declaration of the main method.

public: the method or class in question (e.g. main) is visible to everything.

static: the method in question (e.g. main) is a global method common to all instances of class Greeting.

void: the method in question (e.g. main) does not return anything.

String[] args: args is an array of String objects.  An array is like a vector in Scheme.  We will work more with arrays later in the course.  String is a class that is provided by the Java language to represent character strings.

By convention, the source code of each class should be saved in a file whose name is exactly the name of the class with the extension .java.  Also, by convention, a class name should always start with a capital letter.  For example, the class Greeting defined above should be saved in a file called Greeting.java.

Compiling a Java program from the command line

Use emacs to create and save Greeting.java as shown in the above. In an xterm, go to your directory containing the Java program you just created.

Compile the file Greeting.java with the command

     javac Greeting.java
Then run the program with the command
     java Greeting

What the Java interpreter does at this point is look for the main method in Greeting and execute this method.  As one can see form the code, the main method of Greeting, referred to as Greeting.main(...), accepts  array of String objects as a parameter, but does not make use of it.  The following is an example of a program that makes use of the String array parameter.

public class Howdy {
    public static void main(String[] args) {
        System.out.println("Howdy " + args[0]);
    }
}

Edit, save, and compile the program Howdy.java.  Run the program with the command

     java Howdy
What do you see?  Can you guess what has happened?

Now run the program with the command

     java Howdy Bob
What do you see?  Do you have an explanation for the output?

Now go back to the program Greeting and run it with the command

     java Greeting Bob
Can you explain why the output is what it is?

 

Compiling from inside Emacs

There is another more convenient way to run the edit-compile-debug cycle while using Emacs.

Load the file Greeting.java into an Emacs buffer by the command:

     C-x C-f Greeting.java
Then compile from with Emacs with
     M-x compile
or, equivalently,
     ESC c

In the minibuffer, Emacs will suggest to you the compile command to use. This should be javac Greeting.java, as above. Simply press Return after this. The compilation will begin and the details will be shown in a special Emacs buffer called *compilation*. If there are any errors, they will appear in this buffer. As there are no errors, the compilation should finish successfully and you should see a message like:

Compilation finished at .....
To run the program, now go to the Unix prompt and as before,
     java Greeting
and you'll again see the same output as before.

Warning: if you rename a file, ESC c may suggest the wrong file name. In that case you can edit the file name on the command line before using Return.