Installing Sun Java 2 Development Kit (JDK)
The JDK contains the files and programs necessary to compile and run
java programs. You won't get very far in your CS courses without it.
-
Navigate to the
JDK 5.0 download page at java.sun.com
-
Under the heading JDK 5.0 Update 4 includes the JVM technology
follow the link to Download JDK 5.0 Update 4 (Download the latest
version, it might not be Update 4).
-
Select Accept License Agreement and after the page refreshes
click the link for Windows Offline Installation, Multi-language
-
Once the file has downloaded, run it and follow the installer's instructions.
-
Optional: If you would like to be able to run java from the command
line, you should update some environmental variables, or check that they
have been properly updated.
-
Right-click My Computer and select Properties.
In the Advanced tab, select Environmental
Variables at the bottom.
-
In the bottom section, System variables, look for an entry
called Path and edit it.
-
If there is already an entry including the bin directory in the
location in which you installed the JDK, close the window, you're set.
If not, at the end add the full path to the bin directory in your JDK
directory, make sure you put a semi-colon ';' between the last entry
and the one you enter. For example, a simple Path could have the Variable
value:
C:\WINDOWS;C:\Program Files\Java\jdk1.5_04\bin
-
While you're adding environmental variables, check that JAVA_HOME and/or
JDK_HOME are set to point to your JDK installation directory. For
instance:
C:\Program Files\Java\jdk1.5_04
There, that should have you all set up to start compiling and running
java programs from the command line. You can test things out so far by opening
the run dialog (Win+R or Start - Run) and typing cmd. On the command line,
type:
java -version
you should get output telling you about the
version of the jdk, runtime environment and hotspot client you have installed.
Downloading and Installing Eclipse
Eclipse is a powerful, fully-featured and widely-used integrated development
environment for writing, testing and debugging Java applications. Eclipse
is far more that a program for writing Java code. It is often referred
to as the Eclipse Project. The Eclipse Project encompasses many plugins,
add-ons, sub-projects, toolkits, and all sorts of other bits. A quick look at
the downloads section
will give you a general idea of all that the Eclipse Project includes. You
don't have to worry yourself with anything but the big, bold link at the top
of the page for the Eclipse SDK. The Eclipse SDK includes everything you need
to get going (and more).
-
Go to the Eclipse
downloads section and
download the Eclipse SDK for your platform. For Windows users, this is the
file eclipse-SDK-3.1-win32.zip. If you need to, select a mirror.
-
Extract the zip archive to your desktop using the zip utility built into
Windows XP or using a free program such as
7-Zip
-
Unfortunately, the Eclipse SDK does not include an installer, so you'll have
to set up a couple things manually. Move the extracted eclipse
directory to C:\Program Files
-
Open My Computer and browse to C:\Documents and Settings\All
Users\Start Menu\Programs with another Explorer window open to
C:\Program Files\eclipse\ right-click and drag the eclipse.exe
from the eclipse directory into the window open to the All User's programs.
When the menu pops up, choose Create Shortcut, and rename the shortcut
from Shortcut to Eclipse.exe to Eclipse. You should now be able
to find eclipse in your start menu.
Starting your First Java Project in Eclipse
The following steps will walk you through how to create, write and run a Java
project in Eclipse. Eclipse is a complete Java development platform, and
every Java developer likes to do things their way, so Eclipse will
bombard you with dozens of choices and options, even to create a simple project.
In most cases, the default options are sufficient, but this guide will take you
through, step-by-step telling and showing you exactly what you need to do.
-
Start Eclipse from the shortcut you created in the start menu.
-
When prompted to select a workspace, choose a logical location
that you would like to store your java projects in. A couple of
good options would be a folder called cs122 in My Documents,
or a folder called cs122 on your USB thumb drive. Having separate
workspace for each of your classes is a good idea.
« show/hide image »
-
Next you will be presented with either the Eclipse welcome page or the default
Java perspective. If you see the welcome page, you can just close it with the 'x'
on the tab at the top-left. Eclipse organizes its layout differently depending
on what you're doing. You'll mostly be using the Java perspective, but there
are also debugging, browsing, plug-in, and CVS perspectives. The left pane is
a tree view of your projects, the files they contain and the packages included
from your JDK by default. The main pane is the editor window and where you will
spend most of your time editing your java files. The right pane is an outline
view of the classes, methods, variables and other elements of your program.
Here is a blank java perspective:
« show/hide image »
-
Now to the good stuff. In Eclipse, everything you do will be organized into
projects, from the simplest one-file one-class exercise, to a masters-level
project that contains hundreds of classes in dozens of packages. If you're
doing a bunch of one-file exercises, as long as none of the classes share the
same name, you can group them into the same project; This should not be attempted
for major projects. So to start our new project, we'll click the New
icon and select Project...
« show/hide image »
-
Now we're presented with the new project wizard - Java Project seems
like the logical choice to me; choose it and click next.
« show/hide image »
-
On the next pane, you'll give your project a name. Again, logical naming will
make things much easier on you at the end of the semester when you've got to
find that file... you know... the one with that method... that did that cool
thing... Stay organized, or be doomed to search forever. If you're working on
exercises from the book, you could name your project ch5-arrays or if you're
working on a swing coffee ordering machine in chapter 12, you could call your
project ch12-swing-coffee. Simple stuff, right? It'll make your life a lot
easier. You can leave the rest of the options set to their defaults, unless
you have reason to change them. One thing you may want to change is the JDK
compliance. We've installed JDK 1.5, but it is fairly new so most projects
will default to 1.4 just to be safe. If you'd like to use some of the newer
features of J2SE 5.0 like generics, enhanced for loops, and all the other
things detailed
here,
you can set the JDK compliance to 5.0.
« show/hide image »
-
Clicking next will take you to a pane full of options that mean nothing to you
at this point, so just click Finish. You'll be dropped back into your
Java perspective with your newly created project appearing in the Package
Explorer. You can expand your package to see nothing but the files included
from the JDK.
« show/hide image »
-
Now to create some classes. In this example we'll just create one class called
MyClass in the default package (or in the root directory of the project). With
your project selected, click the New menu and choose class.
« show/hide image »
-
In the Name: field, enter a name for your class. Again, something descriptive,
starting with a capital letter is Java convention. Eclipse will warn you that
the use of the default package is discouraged, do it anyways, but don't tell
anyone I told you to. You can also select the check boxes for generating a main
method if you need one and for generating a few nice comment blocks. Note, that
using packages to organize related classes is usually a very good idea. When you get
on to larger projects or use the same Eclipse project for unrelated classes (like
textbook exercises, for example) it would be good to separate your classes into
packages. Just fill in the package line in the new class dialog.
« show/hide image »
-
Now you've created your class and can fill in your comment block with your name,
and all the information your professor wants. The editing and problem window will
be filled with little cues as you edit - blue to represent stuff you've added, red
lines to indicate syntax errors, and all sorts of other information. Keep an eye
on it as you go about your programming, and you'll soon learn what everything means.
« show/hide image »
-
Now that you've written your program, you need to run it. Right-click on the
project folder in the package explorer and choose Run AS -
Java Application.
« show/hide image »
-
This will launch a progress dialog as Eclipse searches for main types.
« show/hide image »
-
Eclipse will find a bunch of main types in your class as well as in the jdk
classes. Find the name of your class among the mess, select it and click
OK. « show/hide image »
-
You'll now return to your Eclipse workspace and a console pane will appear at the
bottom of the screen containing the output of your program. stdout is in black,
and stderr is in red. If you'd like you can avoid the above two steps by simply
right-clicking your class and choosing Run As - Java Application.
« show/hide image »
-
From now on you will be able to select your project and just click the green
Run toolbar button.
« show/hide image »
-
As you have, or will soon discover, reorganizing java files is a pain. Moving
and renaming files always involve opening those files to rename their classes,
packages, etc. It's this kind of tedium that makes platforms like Eclipse loved
by people who program for a living. Just select the file you want to rename or
move, right-click, choose Refactor - Move or Rename. Eclipse will
take you through the steps to move your files, then make all the necessary changes
for you. « show/hide image »
Saving and Restoring Your Work
Eclipse makes it quite easy to save your work, move to a different computer and start
right back where you left off. The secret is that you must start Eclipse using the
same workspace as you were using before. If you happened to be using the
workspace folder on your thumb drive, just make sure you start Eclipse back
up with the same workspace. A workspace can be copied and moved wherever you like,
and as long as you point Eclipse to it in the Workspace Launcher everything
will be just as you left it. So you could start a workspace on your desktop on your
home computer, move it to My Documents and make some changes, move it to your thumb
drive and make some changes at school and finally move it back to your desktop at
home. Every time you fire up Eclipse pointing to your workspace, everything from your
list of projects, to your open files, even your cursor location will be just as
you left them.
If you'd like to turn in a zipped project, just look for the folder containing your
project in your workspace. In Windows XP just right-click the folder and choose
Send To - Compressed (zipped) Folder. A zip file will be created in the
same directory with the same name as the folder. That's it.
Eclipse is packed full of features that can make your life a lot easier if you take
the time to learn them. Explore the menus and play around. Check out the contents
of the help menu, especially the cheat sheets. Under the Window menu are
the Preferences... which give you fine-grained control over every aspect
of Eclipse. Again, play around and get Eclipse working how you like it.
I hope you enjoyed this guide. If there's anything that could be clarified further,
or if you find any errors or typos, don't hesitate to email me or any PCS board
member.
--
Thomas Achtemichuk