Running Your First Java Program
CMSC 220
Lab 1 Supplement
CSC 220, Spring 2005


The purpose of this exercise is to have you compile and run a small piece of Java code.

Copy Files From The Course Folder

  1. Startup a Xterm Session (see Lab 1)
  2. Create a "folder" or subdirectory in which to copy the necessary files using the Unix "mkdir" command:
    If you haven't already done so, make a www directory:

    bigbird[1]% mkdir www

  3. Change the protection on that directory
     

    %bigbird[1]%chmod 755 www
    This allows you to access this directly from a web browser. We'll explain what this means some other time, or try typing "man chmod".

  4. Move to that folder. The command "cd" allows you to change directories.
    Change into the www/lab1 directory

    bigbird[1]% cd www/lab1

    Now make a subdirectory here

    bigbird[1]% mkdir FirstProject

    bigbird[2]% chmod 755 FirstProject

    bigbird[2]% cd FirstProject

  5. Copy the files from the directory /home/acct3/mmmartin/cs1/Source_Code/Chapter01 to this directory. The *.* means copy all files
    bigbird[3]% cp -r /home/acct3/mmmartin/cs1/Source_Code/Chapter01/*.*    .
  6. bigbird[3]%   mkdir simpleIO
  7. bigbird[3]% cp -r /home/acct3/mmmartin/cs1/Source_Code/Chapter01/simpleIO/*.*    ./simpleIO
  8. Check to see that you have copied three files:
     

    FirstProgram.html
    FirstProgram.java
    PeopleInfo.java

Compile and Run A Java Program

  1. The program "FirstProgram.java" is explained in the course textbook in Chapter 1. It is an applet. Therefore you will run it by using the appletviewer rather than the java application runner. But first you must compile it. To compile this program, you use the compiler: javac.
    bigbird[5]% javac FirstProgram.java
  2. If you view your directory you will notice that two classes have been compiled: FirstProgram and PeopleInfo. Your directory should have the following in it now:
    FirstProgram.class
    FirstProgram.html
    FirstProgram.java
    PeopleInfo.class
    PeopleInfo.java
    You may examine the .java and .html files in xemacs (or any other editor). The .html file contains commands that call on the FirstProgram.class file. This is why you must compile the program before you can run it.
  3. Now, to run the FirstProgram.java applet, use the appletviewer which can view an html file that displays the applet we've just created.
    bigbird[7]% appletviewer FirstProgram.html
    A viewer window will open. You can click on the buttons to view see a demonstration of the applet. When done, click on the window close box on your machine. On a Mac, if the upper left corner is not visible, resize or move the window to access the "Close Box" at the upper left corner to close the viewer.
    For reasons that aren't clear at the moment, appletviewer will not run through MacX. (SIGH). You'll have to view your program through a browser such as netscape. Before you can do this you must set the protections on the files in the directory. You can do this when you are in that directory by typing:
    bigbird[8]%chmod 644 *.*

    Now open a web browser and go to the URL.

    For example: http://www.tcnj.edu/~yourname/lab1/FirstProgram/FirstProgram.html

  4. Now use emacs to edit the FirstProgram.java file. Change the lines in the program so that it gives some information about what you and a friend like about TCNJ.
  5. Save the changed file, compile using javac, and view it using the appletviewer.
  6. Once it compiles and runs cleanly, upload the new java file to your instructor's dropbox for CSC220.