UPenn SEAS: DrJava Introduction

DrJava Primer

Welcome to UPenn's DrJava primer. This guide is intended to get you used to the basics of using the DrJava IDE (Integrated Development Environment).

Running DrJava

First, make sure you have properly installed DrJava (and the DJK). If you need to install one or both, and/or if you need to show DrJava where to find tools.jar, see these instructions.

DrJava logo: big J with stethascope

Once it's installed, DrJava appears as a big J icon (right) and you should be able to open it like any other application. In the lab, DrJava is under the SEAS menu.

With DrJava open, you will have a new file ("Untitled") open. To open an existing file, click on the "Open" button or select File > New (go to the File menu and select New). Note that you'll only ever open .java files in DrJava, although you may indirectly make use of .class, .jar, or image files.

Anatomy of the IDE

The main DrJava window: three panes.

The main window in DrJava is divided into three panes:

The toolbar at the top of DrJava's window, containing new/open/save/close for files and compilation, among others.

The toolbar at the top of the window contains standard editing buttons (opening, closing, saving files; copy, paste, undo; find), as well as Java-specific functions. Of the Java-specific ones, you'll mostly be using Compile — to compile your source code into bytecode — and reset — to reset the interactions pane so you have a clean slate for exercising your program.

Commands from the toolbar are also available under the application menus, along with many other commands; text editing is under the Edit menu, and compile and reset are under Tools.

The Interactions Pane

In the interactions pane, you can evalue expressions or execute statements. This allows you to see how code you write will behave (with the partial exception of Generics. Generics are topic for late(r) in the course). By using the interactions pane, you can create and use objects without needing a main method — that is, without writing a full-blown Java application.

Here's an example of evaluating expressions:

Welcome to DrJava.
> 1+1
2
> 5*2+1
11
> 5*(2+1)
15
> (true || false) && true
true
> 5.0/2.0 == 2.5
true

And, here's an example with execution of statements. Note that putting a semicolon at the end of any code, even an expression like above, suppresses output (treating it like a statement).

Welcome to DrJava.
> int i = 0;
> i
0
> i = 1;
> i = 2*i;
> i
2
> System.out.println("Welcome to DrJava!");
Welcome to DrJava!

The lines with just the variable i are expressions which evaluate to the current value of i. The statement System.out.println(...) calls a provided method which prints out a message (but is not an expression). Its output is also visible under the Console tab.

Finally, here's an example with a simple Counter object (Counter.java):

Welcome to DrJava.
> Counter c = new Counter();
> c.getCount()
0
> c.incrementCount()
1
> c.incrementCount();
> c.incrementCount();
> c.getCount()
3
> c.reset();
> c.getCount()
0

After executing the samples above, the varialbe i (and any other variables you create) will remain defined until you quit and re-open DrJava or reset the interactions pane (with the reset button, the option under the Tools menu, or by way of compiling with automatically resets the interactions).

For command input, note that:

The interactions pane with a script loaded; buttons are visible at right

Finally, it is frequently useful to export interactions for other people to run. You can download sampleinteraction.hist and run it. To do this, save the file, and then go to Tools > Load Interactions History as Script, and select the it. Once loaded, you can click Next to get the next command from the script, and then either click Execute or hit enter/return to execute it.

Writing Code

To write code in DrJava, simply type in the editing pane (after optionally opening files / selecting a file at left). DrJava has syntax hilighting to make it easier to read code; it also auto-indents as you type or if you select a section of code and hit tab.

While editing, useful commands are:

In the preferences, Show All Line Numbers is under Display Options

To make fixing compilation errors easier, you may want to turn on line numbers. Select Edit > Preferences, click on Display Options at left, and check Show All Line Numbers.

Debugging

Compiler errors are displayed in the Compiler Output tab and hilighted in the code.

Compiler errors: The first level of debugging is getting your code to compile. (Often the compiler won't find some errors until you've fixed others.) Compiler errors appear in the Compiler Output tab of the bottom pane, and are hilighted in the source code. (The line number is also given.) Syntax hilighting also helps with catching syntax errors — for example, the reserved word false is turned blue, but the typo fasle is not, since DrJava doesn't recognize it. When compilation is successful, DrJava displays "Compilation completed."

Runtime errors: When running your code from the interactions pane, standard Java errors are displayed. For example, you're likely at some point to see a NullPointerException:

> Example e = new Example();
> e.getString()
NullPointerException: 
  at Example.getString(Example.java:21)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke
	(NativeMethodAccessorImpl.java:39)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke
	(DelegatingMethodAccessorImpl.java:25)
  at java.lang.reflect.Method.invoke(Method.java:585)

Usually, the first line of the Exception's stack trace (bolded here) tells you the line number in your code that generated the problem, from where you can start debugging. (In this case, it was line 21 of Example.java, in the getString method.

Logic errors: Lastly, your code may appear to compile and run fine, but produce bad results due to mis-programming. To combat this, it's best to test each method rigorously to be sure it's doing exactly what you want and expect.

In tracking down logic errors, you may want to use DrJava's debug mode, available through Debugger > Debug Mode, which lets you watch what your code is doing as it runs.