CoSTARS Note #1:
Getting Started with Java and DrJava

1.  A Brief Word about Java and DrJava

In this course we will learn how to program computers using the Java programming language.  We plan to do most of our studies using the CoSTARS learning environment.  However, you should also be able to use a more standard programming environment such as you might see in your next programming course.  For this reason, we will also use DrJava, which is another software package that supports Java programming.

2.  Installing Java and DrJava

If you are in the computer lab at school, then you can skip this step (we did it for you!), but if you are at home, then you will first need to install Java and DrJava.  To install Java:

a)  If you are on a Mac, do nothing -- it comes with Java already installed!
b)  If you are on a PC, then go to the Java Downloads Page
c)  Click on the "Download" button next to "JDK 6 Update 2"
d)  Click on "Accept license agreement"
e)  Click on "Windows Offline Installation"
f)   Save the file to your desktop
     Note:  This is a huge file.  It will take a long time to download.
g)  After the file is saved, double-click on the file on your desktop and follow the instructions from there.
     Note:  Just say yes to whatever questions it asks you.

Fortunately, installing DrJava is easier and quicker than installing Java.  Now, to install DrJava:

a)  Click on this DrJava Download Link
b)  Save this "jar" file to your desktop
c)  That's it.  Now, to run DrJava, just double-click on that "jar" file (and wait a few moments, it can be slow to start)
d)  If you are interested, you can visit the DrJava Home Page for more info and more download options.

3.  Your First Program:  Hello World!

For unknown reasons, it is traditional for your first program in any computer language to simply say "Hello World!".  In keeping with this tradition, we will do that now.  To run the "Hello World" program, follow these steps exactly:

1)  Download (click on the link and save this to your desktop)
2)  Run DrJava (by double-clicking on the DrJava jar file)
3)  In DrJava, open a new file (File/Open) and select the file you just downloaded.
4)  In order to run this program, you must first compile it.  To do this, simply click on the "compile" button and wait a moment.  Soon, you should see a message at the bottom of the DrJava window noting that "Compilation completed".
5)  Now you can run the program:  Click on the "run" button.  You should see the phrase "Hello World!" in green at the bottom of the DrJava window.
6)  If you do not see "Hello World!" in green at the bottom of the DrJava window, raise your hand now!!!  :-)

You did it!  You ran your first Java program!  Great job!!!

Now, you say, what is going on here?  What is that "public static void main"?  How does this work?  Fine questions!  First, let's practice loading, editing, compiling, and running programs a bit more, then we'll get into the details about how they work.

4. Your Second (copy-and-pasted) Program:  "Good morning, Moon!!!"

Now, we'll change your program to say "Good morning, Moon!!!".  To do this, you will copy-and-paste the program provided in these notes.  This is different from the method above, where you downloaded the completed program.  Here are the steps to copy-and-paste a program:

  1. Run DrJava (which you probably are already doing).

  2. Close all open documents (File / Close All)

  3. Copy the program from the notes as follows:
    a)  Select the program using the mouse
    b)  Select "copy" from the menu or simply press "ctrl-c" (that is, hold down the "ctrl" key and then press the "c" key)

  4. Paste the program into DrJava (either Edit/Paste or press "ctrl-v")

  5. Save the program, being sure to give it exactly the name that DrJava chooses (in this case, DrJava will name the file

  6. Now you can compile and run the program as before.

Here is the program:

// Your second program!  This should look very similar to

public class GoodMorningMoon {
   public static void main(String[] args) {
      System.out.println("Good morning, Moon!!!");

5.  Dealing with Errors

If a program has even the smallest error, it will not compile and you cannot run the program until you fix the error.  This includes errors that you may not think of as errors, like replacing a semicolon with a colon or replacing a double-quote (") with a single-quote (').

Worse, while the compiler will helpfully tell you when your program has an error, it often cannot tell you exactly what the error is.  It will try, and sometimes it will be right, but often it will be wrong.  The good news is that, even when it is wrong, the compiler almost always is close.  So you know about  where the error occurred, and you have a hint as to what the error actually is.

Let's see this in action.  For each of the following, first make the change as described, then compile the program with the error and carefully read how the compiler reports the error.  Then, fix the error and continue.

a)  Delete the word "class"
b)  Delete the semicolon (;)
c)  Delete one of the closing braces (})
d)  Change the "S" in "System" from upper-case (S) to lower-case (s)

To continue, for each of the following, download the program and compile it.  Each program should work just like GoodMorningMoon, except each program has an error.  Carefully read the compiler's error message and see if you can find and fix the error in each program.  Good luck!


6.  Next Steps

Next time, we'll start looking at how programs work.  Good job!