Computer Science 15-110, Spring 2010
Class Notes: Installation (Java, DrJava, and JCreator)
- Install the Java
Development Kit (JDK)
- Install DrJava
- Enable DrJava's Line Numbers
- Compile and Run
HelloWorld.java
- Compile and Run
HelloWorldWithErrors.java
- Install JCreator
(Windows-only)
- Enabling Assertions
Installation
- 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):
- Go to http://java.sun.com/javase/downloads/widget/jdk6.jsp
- On the left side, choose your platform (Windows, Linux, etc)
- 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
- Run the installer (double-click on it)
- 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"
- You may then be transferred to a registration web page. You
can safely skip this step.
- If the preceding steps succeeded, then you can safely delete the
file you downloaded in step 3.
- 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:
- Go to http://www.drjava.org/
- Click "Download Windows App" (or Download Mac OS X App) from the
"Current Stable Release" section at the top.
- 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")
- 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.
- 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!
- Compile and Run HelloWorld.java
Now it's time to run your first Java program! To do so, follow
these steps:
- Copy the file HelloWorld.java
to your desktop (or, really, any folder that you choose).
- 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.
- At this point, you should see something like this:
- 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.
- 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.
- 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!
- 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!
- Install JCreator
(Windows-only)
- The main web site for JCreator is here:
http://www.jcreator.com/
(but we won't go there now...)
- Instead, go to the download site here:
http://jcreator-xinox.appspot.com/download.htm
- 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
- Download and install the IDE.
- Run JCreator, select Configure/Options, then select Editor/Java,
then...
- Under "tabs", select "Insert spaces" (not tabs) and set the
"size" to 2
- Also, be sure the "show line numbers" box is selected.
- Enabling Assertions
-
assert Statement does nothing without Assertions Enabled
class MyCode {
public static void main(String[] args) {
assert(false); // does nothing without assertions enabled!
}
}
- Enabling Assertions in DrJava
- Run DrJava
- Select Tools / Preferences
- Select Miscellaneous (on left)
- Check the box (on right) next to "Enable Assert Statement Execution"
- Confirm this worked by running the example code above and confirming
that it throws an AssertionError.
- Enabling Assertions in
JCreator
- Run JCreator 2.5 (adapt these instructions if using a more recent
version)
- Select Configure / Options
- Select JDK Tools (on left)
- Select "Run Application" from drop-down "Select Tool Type" menu
- Select "<Default>" from list on right, and then click on the "Edit"
button on the right
- Select the "Parameters" tab
- 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]
- Click "OK" twice
- 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