[PLT logo] TeachJava 2005

Introduction to DrJava 



DrJava is a lightweight pedagogical environment for Java development created by 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.  

Remember that Java itself must be installed before DrJava can be installed.   To use the latest features in DrJava, be sure to install the latest version of Java.   Version 1.5 of Java can be downloaded from http://java.sun.com/j2se/1.5.0/download.jsp   Be sure to download and install the "JDK" (Java Development Kit) version without NetBeans.   Do not install just the "JRE" (Java Runtime Engine).

Editing

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.

Compiling

Compiler Output Pane: You 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.

Running

Using  the 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.

To manually reset the Interactions pane: Normally, the interactions pane resets whenever anything is recompiled.  This erases all variable declarations that had been made.   The command history (up/down arrows) is preserved however, so that you can easily re-input any statements.   However, sometimes one needs to manually reset the Interactions pane.   This can easily accomplished by right clicking the Interaction pane and selecting "Reset Interactions".  The same menu item is available off the main menu under "Tools".  

Testing

There are many ways to test your code. The most formal way is to use JUnit testing facilities, which we will learn in the next section. For now, you can test your code by interacting with it in the Interactions pane.

Projects

DrJava's Project facility allows the developer to organize their work into a cohesive unit.   It allows the developer to

  • Manage multiple files and multiple packages easily and effectively
  • Distinguish between files that are part of a particular piece of work, separate from other files used for reference.
  • Separate compiled from non-compiled code.

Creating a Project in DrJava

  1. First create a directory that will hold all the code and supplemental materials for your project. Use a folder name that is relevant to the project.
  2. Below that directory, make two subdirectories:
    1. src -- holds all the Java source files.   Put any existing source code you have in here.  If the code is in a package, then put the entire package directory here.
    2. classes -- holds all the compiled class files.
  3. Start DrJava and select Project/New...
  4. Save the project in the project folder you created, using an appropriate name.
  5. Go to Project/Project Preferences and set the Build Directory to be the classes directory you made above.
  6. If you know what class holds the main() function, set the Main Document field to that class in the src directory.
  7. Go to File/Open Folder... and open the src directory.   This will bring in any existing code that you have.

Adding a File to the Project

  • Add a file to the project using File/New... or the speed button as usual.   The file will be added to the project when it is saved with a name.   
  • The same applies for new unit tests.

Compiling the Project

  • Under the Project menu, select Compile Project to compile everything in the project or Compile Open Project Files to compile just the files that are open.
  • The Compile All speed button compiles all files whether or not they are in the project.

Testing the Project

  • Under Projects, select Test Project.

Clearing the compiled files

This feature is useful when zipping up project directories when you don't wish to include the compiled class files.

  • Under Projects, click on Clean Build Directory.  This will erase all files in the classes folder.

 

Language Levels

DrJava supplies 3 "language levels" for pedagogic purposes:  Elementary, Intermediate, and Advanced.    The language levels are a progression of syntax restrictions and code augmentations designed to minimize the number and types of mistakes that beginning students can make.   The Elementary language level is the most syntactically restrictive and performs the most code augmentations, such as auto-generated constructors and accessor methods.  The "Full Java" level has no restrictions or augmentations. 

For more information, see the Language level documenation. 

 


Copyright 2005 Robert Cartwright, Dung Nguyen and Stephen Wong