General Lab Information


[Homepage] | [General Lab Info] | [TA's & Tutors] | [Supplements] | [Homework] | [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.

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. 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] | [Supplements] | [Homework] | [Exams]