Computer Science 15-110, Spring 2010
Class Notes:  Installation (Java, DrJava, and JCreator)


  1. Install the Java Development Kit (JDK)
  2. Install DrJava
  3. Enable DrJava's Line Numbers
  4. Compile and Run HelloWorld.java
  5. Compile and Run HelloWorldWithErrors.java
  6. Install JCreator (Windows-only)
  7. Enabling Assertions

Installation

  1. Install the Java Development Kit (JDK)
    To run Java programs, you need the "Java Runtime Environment", which may already be installed on your machine.  However, to write Java programs, you also must have a Java compiler.  This is a (free) tool that converts Java programs from human-readable form (in .java files) into a more machine-readable form (in .class files).  If you are using a Mac (at least with OS X), there should be nothing for you to do -- the compiler should already be installed (but if it is not, you can get it from here:  http://developer.apple.com/java/download/).  But if you are using a PC, then you have to download the "Java Development Kit" (JDK), which contains the Java compiler.  To do so, follow these steps (very carefully):
     
    1. Go to http://java.sun.com/javase/downloads/widget/jdk6.jsp
    2. On the left side, choose your platform (Windows, Linux, etc)
    3. Click the Download button
      • This is a large (75mb or so) file.  It may take a while.
      • If a pop-up window comes up, you can safely skip it (click on the "x" to close it).
      • When done, you will have downloaded an installer file with a name like this:
        jdk-6u17-windows-i586.exe
    4. Run the installer (double-click on it)
    5. Click accept, next, and so on to get the default installation
      • This may take 5-10 minutes.
      • When completed, you should get a window that says:
        "Java SE Development Kit 6 Update 17
        Successfully Installed"
    6. You may then be transferred to a registration web page.  You can safely skip this step.
    7. If the preceding steps succeeded, then you can safely delete the file you downloaded in step 3.
       
  2. Install DrJava
    Do not proceed with this step unless the previous step succeeded!  Otherwise, your installation will probably not be configured properly.
    Besides the Java compiler, you need a second (free) tool:  an "IDE", or Integrated Development Environment.  This tool includes a special editor tailored to writing code, and other helpful tools for programmers.  There are many choices for Java IDE's, including Eclipse, NetBeans, JCreator, and many more.  It is best for us all to use a common tool, and then one designed specifically for novice students.  And so we will use DrJava.  Even if you have Eclipse or some other IDE already installed, please install and use DrJava here.  To install DrJava, follow these steps:
     
    1. Go to http://www.drjava.org/
    2. Click "Download Windows App" (or Download Mac OS X App) from the "Current Stable Release" section at the top.
    3. Install the downloaded file.
      • On Windows, the file will be named something like: drjava-stable-20090821-r5004.exe
      • Double-click on the downloaded file.
      • Click "yes" if asked to associate ".java" files with DrJava (actually, it's ok to click "no", but you'll probably want to click "yes")
    4. If the preceding steps succeeded, then you can safely delete the file you downloaded in step 2.

    If for some reason this installation fails, you can download just the "jar" file (click on "Download Jar File").  This approach does not require any installation.  Then, each time you wish to run DrJava, you will have to double-click on the Jar file you downloaded.  This option should only be selected if you cannot get the Windows or Mac OS X app installed (say, if you are on a Linux machine).

    Note:  some Linux machines in CMU campus clusters have DrJava preinstalled, but require a patch to work properly.  In particular, you should follow these steps:  In DrJava's edit menu, open the preferences dialog. Paste the following path into the "Tools.jar Location" field:
       /usr/lib/jvm/java-1.6.0-sun-1.6.0.15.x86_64/lib/tools.jar
    (There is some chance you'd need a similar-but-slightly-different path on different machines, depending on their configurations.)  Apply the change, and close DrJava. When you re-open DrJava, it should successfully find the compiler and work correctly. Each user should only need to follow this procedure once ever.

     

  3. Enable DrJava's Line Numbers
    It turns out to be really handy to have DrJava include line numbers in your programs.  To enable this feature, first run DrJava.  Then, from the Edit menu, choose Preferences.  On the left, choose "Display Options".  Now on the right, be sure that "Show all line numbers" is selected.  That's it!
     
  4. Compile and Run HelloWorld.java
    Now it's time to run your first Java program!  To do so, follow these steps:
     
    1. Copy the file HelloWorld.java to your desktop (or, really, any folder that you choose).
    2. Open that file with DrJava
      • If you selected "yes" with your file associations, you can just double-click on that java file to edit it.
      • Otherwise, you should run DrJava, then select File/CloseAll if any files are open, then select File/Open and navigate to the HelloWorld.java file.
    3. At this point, you should see something like this:

       
    4. The colored text to the right of the line numbers is your first Java program!  Never mind the details for now.  Just know that it is a valid Java program that should print out the words "Hello World!" when you run it.  Now, how to run it?  Well, before you run a Java program, you must first compile it.  In this step, DrJava will check for various errors, and if everything looks ok, it will translate your program into a more machine-readable form.
    5. Click on the "compile" button.  After a brief delay, the message "Compilation Completed" should appear in the lower-left of DrJava.  Also, though you may not see it now, DrJava created the file HelloWorld.class in the same directory where HelloWorld.java is.
    6. Now you can click on the "run" button.  You should see the text "Hello World!" appear in the lower-left of DrJava.  If so, then you ran your first Java program!  Congratulations!
       
  5. Compile and Run HelloWorldWithErrors.java
    Before we get going in earnest, you should see what happens when there are problems with your program.  To that end, edit the file HelloWorldWithErrors.java in DrJava (following the same steps as with HelloWorld.java).  This version of HelloWorld has some errors.  Go ahead and try to compile the file.

    You should see that the compile failed.  There should be bright yellow text explaining why the compile failed.  Read this text!  Any time you have an error, carefully read the text explaining the error.  In this case, it says (at some point):
       HelloWorldWithErrors.java:5:  ';' expected
    This contains lots of useful information!  It tells us the file name and line number (5) on which the error occurred.  It also tells us the actual error -- a semicolon was expected but not found.  This is because we deliberately deleted the semicolon from the end of that line (after the parenthesis, and before the green text).  This kind of error is called a syntax error.  Our program is not valid Java syntax, and so it cannot compile.

    To fix this, add that semicolon back, so the line looks like this (with the added semicolon highlighted here):

    System.out.println(1/0); // <- missing semicolon!

    And now compile again.  This time, the compile works.  Great!  So now run the program.  Instead of printing "Hello World!", this program now tries to print the result of dividing 1 by 0.  Now, you can't divide 1 (or any other number) by 0 (right?).  So we get an error.  Look carefully at what the program displays when you run it:

    java.lang.ArithmeticException: / by zero
         at HelloWorld.main(HelloWorldWithErrors.java:5)


    This is not a syntax error.  After all, the program did compile and it ran.  This is called a runtime error.  The program compiled and ran, and then it crashed.  When that happens, Java usually prints out a very helpful error message.  In this case, Java told us that it crashed because it tried to divide by zero, and it even tells us on what line (5) of which file this happened.  How useful!  In this case, we placed that error there on purpose.  In most cases, the error is there by accident, and now we know where to fix it!

    When you have errors -- and you will have errors -- be sure to carefully read the error messages both for what happened and where it happened.

    Now, about the compiler telling us the actual error: this is sometimes true, and sometimes not.   Unfortunately, sometimes it gets the cause of the error wrong. Other times, it gets the error right, but the way it explains the error is basically incomprehensible.  Even so, it usually is correct about where the error occurred!  Moral:  even if you cannot make heads or tails out of the error message, read it carefully, and definitely look at the line number it mentions!
     
  6. Install JCreator (Windows-only)
    1. The main web site for JCreator is here:  http://www.jcreator.com/  (but we won't go there now...)
    2. Instead, go to the download site here:  http://jcreator-xinox.appspot.com/download.htm
    3. We recommend the simplest version, JCreator Classic:  LE v.2.5.
      However, if you are using Windows Vista, you may need to use  JCreator LE v.4.5
    4. Download and install the IDE.
    5. Run JCreator, select Configure/Options, then select Editor/Java, then...
      1. Under "tabs", select "Insert spaces" (not tabs) and set the "size" to 2
      2. Also, be sure the "show line numbers" box is selected.
         
  7. Enabling Assertions
    1. assert Statement does nothing without Assertions Enabled
      class MyCode {
        public static void main(String[] args) {
          assert(false); // does nothing without assertions enabled!
        }
      }
    2. Enabling Assertions in DrJava
      1. Run DrJava
      2. Select Tools / Preferences
      3. Select Miscellaneous (on left)
      4. Check the box (on right) next to "Enable Assert Statement Execution"
      5. Confirm this worked by running the example code above and confirming that it throws an AssertionError.
         
    3. Enabling Assertions in JCreator
      1. Run JCreator 2.5 (adapt these instructions if using a more recent version)
      2. Select Configure / Options
      3. Select JDK Tools (on left)
      4. Select "Run Application" from drop-down "Select Tool Type" menu
      5. Select "<Default>" from list on right, and then click on the "Edit" button on the right
      6. Select the "Parameters" tab
      7. Edit the "Parameters:" value by adding "-ea " (with a trailing space, but without the quotes) to the front.  For example, after I finished this task, my Parameters: field looked like this:
        -ea -classpath "$[ClassPath]" $[JavaClass]
      8. Click "OK" twice
      9. Confirm this worked by running the example code above and confirming that it throws an AssertionError.

carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem