Lab 2 - The DrJava Development Environment


This lab introduces the DrJava programming environment. You will use DrJava to develop and test programs for both lab and homework assignments.


        $ mkdir nhLab


DrJava is an easy to use software development environment for writing Java programs. It was created by the Computer Science Department at Rice University, and is currently under active development by the JavaPLT group at Rice. DrJava is designed primarily for students, and provides an interactive facility for developing Java code. It includes an editor and debugger, and incorporates the JUnit testing framework and javadoc documentation generating tool.

DrJava is discussed in Appendix II of the text. You can download DrJava for use on your home system from or

Starting DrJava

        $ drJava

DrJava's main window is divided into three panels. The largest area to the right is the definitions pane. You write and edit class definitions in the definitions pane. The open document list displays a list of open documents. You can view and edit a particular document by selecting it from the list. When you click on a document in the list, it will be displayed in the definitions pane. The bottom panel contains several tabbed panes. Clicking the tab displays the pane. For the present, we will be concerned with the interactions pane and the compiler output pane. You can enter and evaluate java statements and expressions in the interactions pane. Compilation results are displayed in the compiler output pane.

Configuring DrJava

Since both DrJava and Java are being actively developed, the preference options available may not be exactly as described here. Furthermore, some of the options will have been set for you already. Your lab instructor will tell you what you need to do to properly configure DrJava. DrJava will remember your option settings. Options do not need to be set every time you run DrJava.

        -tag require -tag ensure

Exercising the interactions pane

        > int i;
        > i = 17;    
        > i
        > i = 18;    
        > i
        > i+1
        > i*2
        > float f;
        > f = 17000000;
        > f
        > f+1
        > f == f+1
        > i = 17000000;
        > i == i+1
        > i = f;

The definitions pane

There are several things you should notice. First, the text is displayed in several different colors. Each color denotes a different kind of "lexical component." For instance, words like "public" and "class" are key words. Key words are displayed in a unique color. The color coding will help you recognize keying mistakes when you key a program.

Second, the text is indented in a uniform way. This indention reflects the structure of the program and makes it easier to read. When you key in a program, DrJava automatically indents for you. If the indention is not what you expect, it is often a sign that you have made a syntactic mistake.

Finally, DrJava will show matching parentheses, braces, and brackets by highlighting. Again, this will help you find and avoid mistakes when you key a program.


You will notice that each line in the display pane is now numbered. Error messages often mention a line number. You can also navigate to line by number by choosing the Go To Line... option from the Edit pull-down menu, or by keying Control-G. (The line numbers do not need to be displayed to do this.)

You can also navigate the display pane by choosing Find/Replace from the Edit pull-down, or by keying Control-F.


You will notice that the document name "Counter" is followed by an asterisk "*" in the open document list. This means that the document has been modified in the definitions pane but the changes have not been saved to disk. (You modified the document when you added and removes the spaces.)

Next, you will compile the class definition: that is, invoke a program called a compiler that will translate the source into "machine level" code that can be interpreted by the Java interpreted. (The interpreter is called the "Java virtual machine.") The compilation process will produce a new file named Counter.class in your lab2 directory.

Something is wrong. Notice that the Compiler Output tabbed pane at the bottom of the window is selected, and it indicates that five errors were found. The first error is highlighted, as well as the line in the definitions pane that caused the error.

If you read the error messages and look at the highlighted lines, you will notice that they all have something to do with the symbol "count". The problem is that we have forgotten to define this variable. (Don't be concerned if you don't understand this yet. This exercise is just to get you familiar with using DrJava!)

        private int count;
         * A simple integer counter.
        public class Counter {
          private int count;
           * Create a new Counter, with the count initialized to 0.

Generating a specification document

As mentioned above, Javadoc produces a collection of HTML documents. These are usually viewed with a browser.

Testing a class

JUnit is a framework we will use to test our classes. Unfortunately, as this is written DrJava does not yet support the latest version, JUnit 4. The Test button on the tool bar will run an earlier version which we will not use.

You can obtain the latest version of JUnit for your personal system from

        > java org.junit.runner.JUnitCore nhLab.lab2.CounterTest
        Tests run: 3, Failures: 2
        There were 2 failures:
        1) testInitialState(nhLab.lab2.CounterTest)
        java.lang.AssertionError: expected:<1> but was:<0>
           * Create a new Counter, with the count initialized to 0.
          public Counter () {
            count = 1;
        OK (3 tests)

Running a program from DrJava

You might want to spend a few minutes experimenting with some of the options available in the Edit and Tools pull-down menus.

What to submit

If directed by your instructor, turn in output from the browser, and listings of the modified Counter class and the interactions history for the session. Be sure everything you turn in is clearly labeled with your name and section number.

You can print the interactions history as follows.

        $ lp ~/nhLab/lab2/temp.history