General Lab Information


[Homepage] | [General Lab Info] | [TA's & Tutors] | [FAQ's] | [Supplements] | [Homework] | [Excellent Programs] | [Exams]

Note: For lab times goto [TA's & Tutors] page.

Editing a Program

You can use your favorite text editor for your Java programs. The source file should end in the extension .java.

Using tio in the labs

You can use unix.ic.ucsc.edu, or any of the workstations in BE105 or Crown. To automatically include tio you will need to also set your CLASSPATH. The simplest thing to do is add the following line to your .cshrc file (don't miss that dot at the start of that file name). You should have a .cshrc file in your home directory. If not, create one.
setenv CLASSPATH .:/afs/cats.ucsc.edu/courses/cmps109-cm/java
Don't miss the first two characters of the path which are dot colon. Make sure there is a newline at the end of the line. The first time you will need to log out and back in, or just type:
source .cshrc

Compiling a Program

The command to compile a Java program is ``javac''. If you had a program called Hello.java, you could compile it using the command ``javac Hello.java''.

Running a Program

After you have "compiled" your program, you run it using the Java Virtual Machine which is called simply, "java".

For example, if I compiled my ``Hello.java'' program using the command ``javac Hello.java'', I would type ``java Hello'' at the UNIX prompt to run my program. Notice that it is not "java Hello.java" or "java Hello.class".

Debugging Programs

If, when you run javac, you get a whole slew of compiler error messages, work on fixing the first couple of errors and then re-compile; the later error messages probably result from javac getting confused after the first error.

If you are on a terminal that doesn't let you scroll back to see the first errors, you can redirect the error messages to a file by using >&. To name the error file errs, type ``javac Prog.java >& errs''; use ``head errs'' to see the first ten lines of that file or ``more errs'' to scroll through the file.

If things are getting really tough, you can use a debugger. All Integrated Development Enviroments (IDEs) have built in debuggers that are very nice. For beginning students, the overhead of figuring out an IDE can be more trouble than it is worth. There is a simple debugger that comes with the standard Java Development Kit (JDK), which we are using. It is called jdb.

If your program is throwing an exception and the message gives you a stack trace but it says "compiled code" instead of showing you the line numbers of where you were in your program, trying running your program with the command:

    java -Djava.compiler=NONE YourClassFile

Using jdb

To start debugging your program first compile it with "javac -g MyProg.java". Notice the "-g" flag. This causes the compiler to insert debugging information. Then type


    jdb MyProg
    stop in MyProg:main
    run
    list
This should show you the first few lines of your program, with an arrow pointing at the first executable statement in main(). Type help to get a complete list of debugger commands. Here are a few.

For more information view Sun's documentation for jdb.

You might also want to look through the CMPS12L lab on using jdb.


[Homepage] | [General Lab Info] | [TA's & Tutors] | [FAQ's] | [Supplements] | [Homework] | [Excellent Programs] | [Exams]