COMP 310  Java Resources  Eclipse Resources

(Back to Connexions Modules Home)

Summary: A short tutorial on using DrJava.

DrJava is a lightweight pedagogical environment for Java development created by the Programming Languages Team (PLT) at Rice University. DrJava provides a way to edit and save java code with key words highlighting, curly brace matching, and an interactive environment to manipulate objects and test code without having to write the main method. It can be freely downloaded from the web. Please see the DrJava home page

Dr Java Tutorials

On the main DrJava web site, one can find documentation and tutorials on its use:

  • Quick Start Guide: This guide covers the most important aspects of using DrJava, such as opening and saving files, compiling, testing, debugging and setting preferences.
  • Full Documentation: This is the full documentation for DrJava, covering all aspects of the system.
  • FAQ: This Frequently Asked Questions list covers many of the common issues that come up when running DrJava.
  • Video Tutorials: Some short introductory Flash vidoes on installing Java and DrJava and running DrJava. This is a good place to start for beginnings to get a feel for what can be done with DrJava. Note: The videos may not run in Internet Explorer, if not, please try accessing the videos using another browser, such as Firefox.
Below are some short descriptions of the various parts of DrJava.


Definitions Pane: When you run DrJava you will see a window appear. This window (GUI) consists of four subwindows. The top half of the GUI constitutes the Definitions pane. You type in all the class definitions here. After you type in some code, you need to click on the save button before you can compile your code. All the classes in the Definitions pane will be saved in a single file. There should only be one public class in the Definitions window, and the saved file should have the same name as that of the public class with the extension .java.


Compiler Output PaneYou compile your Java code by clicking on the Compile All button in the menu bar at the top. Every time you compile your code, DrJava will display all compile error messages here. Clicking on an error message will highlight the line where the error is suspected to take place in the Definitions pane. If there is no compile error, DrJava will declare success in this pane.


Interactions pane: There are several ways to run your Java code. For now, we will restrict ourselves to the Interaction pane at the bottom of the main GUI window. This is where you can type in any valid Java statement. Usually, you would type in code to instantiate objects of classes defined in the Definitions window, and call their methods to check whether or not they perform correctly. Typing a valid Java expression terminated with a semi-colon and then pressing the Return (Enter) key, will cause DrJava to evaluate the expression but NOT printing the result. If you want DrJava to print the value of the result in the Interactions window, you should press Return without terminating the expression with a semi-colon. There is a menu item to reset (i.e. clear) the Interactions window. Another way to clear the Interactions window is to force a re-compile by editing the Definitions pane. If your code has printing statements, the output will be displayed in the Console Output pane.


There are many ways to test your code. The most formal way is to use JUnit testing facilities, which is covered in a separate module: Unit Testing with JUnit in DrJava. For simple tests, you can test your code by directly interacting with it in the Interactions pane.


To debug a program in DrJava, first put DrJava into Debugging Mode by setting the check box under the Debugger on the main menu. This will enable all the other debugging features. When debugging mode is active, the debugging pane will appear near the bottom of the DrJava window. This pane contains the Watch window and tabs that show the current Stack and Threads status. The debugging pane also has buttons for Resume debugging, Step IntoStep Over and Step Out. These features are described in detail below.

The basic technique for debugging is to set breakpoints on lines of code that are of interest/problematic and then to either step slowly through the code execution from that point and/or to examine the values of various variables.

Breakpoints: Under debugging mode, DrJava will stop the execution of a program whenever a breakpoint is encountered. Execution stops before the breakpoints line of code is run. To set or clear a breakpoint, place the cursor on the desired line of code and either use the Debugger/Toggle Breakpoint on Current Line or by simply pressing Ctrl-B or by right-clicking the desired line and selecting Toggle Breakpoint. Note that it only makes sense to put a breakpoint on a line of code that actually executes, which excludes

  • Blank lines
  • Comments
  • Method signature lines
  • Lines with a single curly brace on them.
  • Field/attribute declarations that have no initialization code.

To clear a breakpoint, follow the same procedure used to set the breakpoint.

Whenever the program execution is stopped on a line of code, the Interactions pane can be used to examine the value of any variable that is in scope or even to execute methods on those variables (if they are objects). You can even set the values of any available variables by assigning a new value to them. This is useful if they have the wrong value for some reason and you want to check if the rest of your code will run properly if the correct value is present.

Clear All Breakpoints: Clicking on Debugger/Clear All Breakpoints will clear all breakpoints that have been set in your program.

Clicking on Debugger/Breakpoints or pressing Ctrl+Shift+B will bring up a tabbed pane at the bottom of the DrJava window that enables you to conveniently examine, navigate to, delete, or disble (without deleting) all of the breakpoints in your program.

Step Into: When execution has stopped on a line of code, clicking on the Step Into button in the debugging pane or selecting the menu item Debugger/Step Into or pressing F12 will cause the current line of code (highlighted in blue) to be executed. If the current line of code involves the invocation of a method, then the current line of code will be advanced into the first method to be called, as per Java's order of execution rules. If the current line of code is the last line in a method, the current line of code will become the next executable line of code in the method that called the current method. Note that if the caller's line of code involved multiple method calls, then the current line of code will return to that line of code in the caller and then execution will advance into the next method to be called, or if the current method was the last method to be called, the current line of code will be the next line of code in the caller.

Step Over: When execution has stopped on a line of code, clicking on the Step Over button in the debugging pane or selecting the menu item Debugger/Step Over or pressing F11 will cause the current line of code to be executed. This is very similar to Step Into, but if the the execution will not stop inside of any methods called in the current line of code. Thus the new current line of code will always be in the same method, unless the executed line was the last line in the method, upon which execution will stop in the calling method as usual. This feature is very useful when you are confident that the methods being called work properly and you are not interested in their detailed execution.

Step Out: When execution has stopped on a line of code, clicking on the Step Out button in the debugging pane or selecting the menu item Debugger/Step Out or pressing Shift+F12 will cause the execution to resume until the current method exits. Excution will stop at the next executable line in the calling method. This is very useful to quickly exit a method whose detailed execution no longer interests you (for instance, if you accidentally stepped into a method that you wanted to step over).

Resume Debugging: When execution has stopped on a line of code, clicking on the Resume button in the debugging pane or selecting the menu item Debugger/Resume Debugging or pressing F7 will cause the program execution to resume and continue until the next breakpoint is encountered. This is useful when you are no longer interested in the detailed execution of the code until the next breakpoint.

Watches: Watches are used when you want to continuously monitor the value of a particular variable. Simply type the name of the variable or field into the Watch pane and when code execution has paused, if that variable name is in scope, its value will be displayed.

Stack: The stack contains an ordered list of all methods with pending operations at any given point of a program's execution. That is, when the program execution has stopped at a particular line of code, the stack contains the method that line of code is in, the method that called that method, the method that called that method, etc. In DrJava, the Stack tab will show the stack with the current method at the top and the calling method below it. Looking at the stack is very useful for determining where in your program you actually are, as often a method is called from locations that are unintended or one discovers that a method is not being called from where you wish it to be called.

Threads: A Java program run on many simultaneous execution paths or "threads", each performing different tasks necessary for the total operation of a Java application. A simple program written by a programmer may have a single thread that runs the code written by that programmer, but may also have many other threads performing such tasks as managing the graphical user interface ("GUI") or managing the memory allocated to the program. It is also possible for a programmer to write code that utilizes multiple threads of execution to accomplish the desired goals. The Threads tab is most useful for debugging these multi-threaded programs by enabling the developer to better understand which threads are accessing which objects.


Interactions pane:
The pane at the lower edge of the DrJava window where one can interactively execute Java statements.
Definitions pane:
The pane at the upper right of the DrJava window where one edits class definitions.
Unit Test:
The testing of a single class or small collection of classes (a "unit") to verify correct behavior at a fine-grained level.



Originally published in Connexions (CNX): https://web.archive.org/web/20090221211751/http://cnx.org:80/content/m11659/latest/

© 2023 by Stephen Wong