CS Resources—Java; Debugging Tutorial
Topics
- How to Compile and Run a Java Program in Unix
- How to Debug a Java Program
- Testing Your Debugging Skills
- Using Emacs to Debug & Edit Your Programs
How to Compile and Run a Java Program in Unix
-
The first step is to write a program that you will compile. To save
some time, we already have a couple of test programs set up for you to
experiment with. You should put the files inside a subdirectory of your
cs241 (or cs230) directory. Type the following:
- cd (this will take you to your home directory)
- cd cs241 (or cs230 - such a directory should be automatically
created when you register for the course. If you don't have one, create it)
- mkdir jdb
- cd jdb
- cp ~cs-marks/pub/resources/java/jdb/*.java . (don't forget the
final period - it tells UNIX to copy the files to the current directory)
-
At this point if you type ls you should see two files in the directory. These files
are also available on the Web:
-
Now we've got a couple of programs to compile. First let's look at
the factorial program. Open the program using your favourite text
editor. For the remainder of the tutorial it is assumed you are using
pico, although you may want to use a different editor such as Emacs (and
you should certainly use a different editor for your assignments). Type
pico fact.java
-
Take a look at the code (it is not very long). It uses recursion
to solve the problem - not very efficient, but it is perfect for what we
are going to do. Manually trace through the code to get an idea of
what it is doing. When you are done looking at the code, press Ctrl-X
to quit the program.
-
To compile the factorial program, type in
javac -g fact.java
The -g option tells the compiler to include debugging information for use
by the debugger in the next section.
-
This should have created a java bytecode class file: fact.class. You
can verify this by using the ls command. To run the
program you need to run the java interpreter with the name of the class
to run along with any parameters that the program expects. The factorial
program expects an input of 1 parameter, the number to calculate the factorial
value for. Type
java fact 1
to calculate 1!
-
Run the program a couple more times, with different input (try a negative
number for instance). Don't bother running the program with input
of much greater than 10, as the factorial numbers grow really fast, and you
start to get overflow.
How to Debug a Java Program
Once your Java program has compiled you may need to debug it. The most important things
you need to do is to find out the values of variables in your code and monitor the execution
of the code, particularly the calling of procedures and functions.
The java System.out.println statement is and effective way to debug a program.
Placing "debugging printouts" throughout the program at critical places
to monitor the execution can be helpful. These printouts
can easily be commented out when the debugging is completed, and later
uncommented if further bugs show up later.
But what to do when this becomes too tedious? Another option is to
use a debugger, such as jdb. We'll go through an example which takes you
through some basic debugging tasks. This tutorial goes through using
the text version of the debugger. At the completion of the tutorial
you should be comfortable enough with the different debugging techniques.
For those students that want the debugger to be a little more visual, there
is a further tutorial that teaches you how to integrate jdb and Emacs together
into a development tool (see the bottom of this tutorial).
Test Your Debugging Skills
Now it's time to try your hand at debugging some code. We will now
look at the fib.java program which produces the desired Fibonacci
number using the same recursive method as the last program. However, there
are two errors in the program. The first is a bug that prevents the
program from properly compiling, and the other is a logic error in the
code.
-
Try to compile the Fibonnaci program:
javac -g fib.java
-
You will receive an error message describing an error at line 15 pertaining
to fact not being declared. Open up the program with your favourite editor and go
to line 15. In Pico, you can find out what line you are on by pressing Ctrl-C
in the editor. The message indicated that there is a problem with
this declaration. Do you see the problem? Fix the error and
save the changes. In case you can't figure out the error, the answer appears
at the bottom of this document.
-
Now compile the program and run the program by typing java fib 0
-
Run the program with an input of 0 and 1. So far so good. Now
run the program with an input of 4. You will see an unending streak
of errors, with no indication of where the error is. Press Ctrl-C
to kill the program (it may take a few seconds).
-
Now it's time to play detective. Load up the debugger and step through
the code for the calculation of the Fibonacci number (just like you did
last time), and see if you can narrow down the problem. There is
only 1 tiny bug in the actual code. Once again, there is a hint that
appears at the bottom of the tutorial, but you should be able to solve
the problem on your own.
-
Make the change to the code and recompile and test the change. Does
your program produce an output of 8 for in input of 6?
Solutions
Solution to compile error! To make the solution appear,
highlight the space under this text.
The compile error has to deal with the fact that the class is not
properly named the same as the file name. The class should be
called fib, not fact.
Hint to logic error! To make the hint appear, highlight the space
under this text.
What are the values of the parameters being recursively
passed into the Fibonacci method? Will these calls to Fibonacci ever
terminate?