| [Page 16 (continued)]|
2.2. Introduction to DrJava
We recommend that you program using a tool called DrJava. DrJava is a simple editor (tool for entering program text) and interaction space so that you can try things out in DrJava and create new programs (methods) and classes. DrJava is available for free under the DrJava Open Source License, and it is under active development by the JavaPLT group at Rice University.
If you don't wish to use DrJava, you can use this book with another development environment. Simply set the classpath (place to look for classes that you are using in your program) to include the classes used in this book. Check your documentation for your development environment to see how to do this. We recommend using DrJava because it is free, easy to use, has an interactions pane for trying out Java statements, is written in Java so it works on all platforms, and it includes a debugger. Since it is free, you can use it just for the interactions pane, and do your coding in another development environment if you prefer.
To install DrJava, you'll have to do these things:
Make sure that you have Java 1.4 or above installed on your computer. If you don't have it, load it from the CD, or you can get it from the Sun site at http://www.java.sun.com.
You'll need to install DrJava. You can either load it from the CD or get it from http://drjava.org/. Be sure to get a version of DrJava that works with the version of Java you are using! See the CD for more information on installing DrJava.
Add the Java classes that come with the book to the extra classpaths for DrJava. Start DrJava (see the next section for how to do this), click on EDIT and then PREFERENCES. This will show the Preferences window (Figure 2.1). Click on the ADD button below the EXTRA CLASSPATH textarea and add the path to the directory where the classes that come with the book are, such as: c:/introprog-java/bookClasses.
Figure 2.1. DrJava Preferences window.
2.2.1. Starting DrJava
How you start DrJava depends on your platform. In Windows, you'll have a DrJava icon that you'll simply double-click. In Linux, you'll probably cd into your DrJava directory and type a command like java -jar drjava-DATE-TIME.jar where DATE-TIME are values for the release of DrJava that you are using. On the Macintosh, you'll probably have to type commands in your Terminal application where you cd to the correct directory then type ./DrJava. See the instructions on the CD for what will work for your kind of computer.
Common Bug: DrJava is Slow to Start
DrJava will take a while to load on all platforms. Don't worryyou'll see (Figure 2.2) for a long time. This is called a splash screen, which is a small picture that displays while a program is loading. If you see the splash screen (Figure 2.2), DrJava will load.
Figure 2.2. DrJava Splash Screen.
Common Bug: Making DrJava Run Faster
As we'll talk more about later, when you're running DrJava, you're actually running Java. Java needs memory. If you're finding that DrJava is running slowly, give it more memory. You can do that by quitting out of other applications that you're running. Your e-mail program, your instant messenger, and your digital music player all take up memory (sometimes lots of it!). Quit out of those and DrJava will run faster.
Once you start DrJava, it will look something like Figure 2.3. There are three main areas in DrJava (the bars between them move so that you can resize the areas):
The top-left window pane is the files pane. It has a list of the open files in DrJava. In Java each class that you create is usually stored in its own file. Java programs often consist of more than one class, thus more than one file. You can click on a file name in the Files pane to view the contents of that file in the top right window pane (definitions pane).
The top-right part is the definitions pane. This is where you write your classes: a collection of related data and methods. This area is simply a text editorthink of it as Microsoft Word for your programs. The computer doesn't actually try to interpret the names that you type up in the program area until you compile. You can compile all the current files open in the files pane by clicking on the COMPILE ALL button near the top of the DrJava window. Compiling your code changes it into instructions that the computer understands and can execute.
The bottom part is the interactions pane. This is where you can literally command the computer to do something. You type your commands at the > prompt, and when you hit return, the computer will interpret your words (i.e., apply the meanings and encodings of the Java programming language) and do what you have told it to do. This interpretation will include whatever you typed and compiled in the definitions pane as well.
Figure 2.3. DrJava (with annotations). (This item is displayed on page 19 in the print version)
There are other features of DrJava visible in Figure 2.3. The OPEN button will let you open a file, it will add the file name to the files pane, and show the code in that file in the definitions pane. The SAVE button will save the file that is currently displayed in the definitions pane. The JAVADOC button creates HTML documentation from the Javadoc comments in your files (comments that start with '/**' and end with '*/'.
Making it Work Tip: Get to Know Your Help!
An important feature to already start exploring is the HELP. If you click on HELP and then click on HELP again when a menu is displayed you will see a help window. Start exploring it now so that you have a sense for what's there.