Editing,
compiling, running and testing in Eclipse (and more) |
Overview
Programming with tools can be a tedious and at times frustrating activity.
(On the other hand, programming without tools is demented and would make
Hercules' cleaning of the Augean stables seem like learning to play Angry
Birds.) This page is intended to lay out some of the key tools that you will
need in 331. (Many of the terms have links to describe them in more detail;
many of these are links to Wikipedia, but there are lots of other references
you can find on-and-off the web.)
All the programming this quarter will be in Java. To write and run Java
programs requires ways to
Java programs run on a Java Virtual Machine (JVM) that executes Java bytecode. There are JVMs for almost all computing platforms including Linux, Windows and Mac OS. A Java Development Kit (JDK) is used to develop and compile Java programs; a JDK has lots of tools, including the compiler (javac),a document generator (javadoc), and more. A JDK also contains a Java Runtime Environment (JRE) (or "private runtime"), which includes a JVM as well as all the class libraries one needs to both run and develop Java programs. Editing, modifying, testing, debugging, etc. is often done using an interactive programming environment (IDE), such as Eclipse and NetBeans for Java and Visual Studio for C, C++ and C#. Some people instead to choose to use a less-integrated set of tools, such as an editor (like emacs), a debugger (like jdb), a compiler (like javac), etc., combining them primarily by executing them directly from the command line of their operating system.
We will assume you're using Eclipse—if not, you're (much more) on your own.
We will be using Java Platform, Standard Edition 6 (Java SE 6) -- the "product version" (which you may also see called Java 1.6, as the "developers" version). (The release before this was, confusingly, Java 2 Platforn, Standard Edition 5.0, J2SE 5.0, also known as Java 1.5.) Don't forget a couple of things to configure before you get started with the 331 assignments.
UW CSE Instructional Computing
For most work, you'll be able to work on whatever standard operating system
platform you choose. In a few cases, you may need a Linux prompt to perform an
action on attu
. In these cases, you'll need to use ssh (a secure shell) to access
attu
and learn some basic knowledge of Unix/Linux commands, which
you can get through the Unix tutorials
provided by the student ACM chapter.
Oh yes, you'll also absolutely positively need to remember your UW CSE user id and password. If you need help, take a peek at the general overview of CSE's computing support for undergraduates.
Working at Home (warning! warning! warning!)
Some of you will naturally prefer to work at home rather than in the CSE labs. We have a handout on how to do this, but it comes with a major warning. Installing and running Java -- and especially Eclipse -- on different operating systems (variants of Linux, Windows, Mac OS) with different versions of Java, Eclipse, and such can be complicated. We have some expertise, but it's sometimes hard for us to get it right and, even more, figure out what's wrong. We will try to help, but we will be much more helpful overall in addressing any problems that arise in the UW CSE installations in the lab.
In the Java perspective (Window » Open Perspective » Other... »
Java
), you can open files by double-clicking them from the Package
Explorer
pane. You can switch between multiple open files quickly by
using Ctrl-F6
to select the file you want from a dropdown list.
You can also navigate through different files using the tabs on top of the
editor pane.
To create a new Java source file (with the .java
extension),
select from the top menu File » New » Class
. In the window that
pops up
Assignments331/src
and select a
package such as a0
MyClass
) and click
Finish
.main
method that Eclipse will generate automatically if you
check the appropriate checkbox in the New JavaClass
screen.Create non-Java files, such as text files, in a similar way. Select
File » New » File
, choose the parent directory of your new file in
the dialog box, and type the desired filename.You can create a new directory by
simply appending the directory name in front of your desired filename.
Eclipse has an extensive and largely effective auto-complete engine, which
can significantly improve your effectiveness. The idea is to allow you to
enter the prefix of an element (such as a variable name), with auto-complete
providing legal and likely completions. CTRL-Space
invokes
auto-complete at the cursor location in the active editor. For example, if you
have declared a variable named spanishGreeting
in the current
class, and have typed the letters spanishGree
in a subsequent
line, Eclipse can infer that you mean to type spanishGreeting
. If
you enter CTRL-Space
while your cursor is at the right of the
incomplete name, you should see spanishGree
expand to
spanishGreeting
.
Other examples:
ArrayL Ctrl-Space --> ArrayList
random.next Ctrl-Space --> random.nextInt
new ArrayList( Ctrl-Space --> popup
menu of ArrayList's
constructors
random.nextInt( Ctrl-Space --> tooltip
showing nextInt's
parameters
class Foo extends Bar { Ctrl-Space
--> menu of Bar's methods that
can be overridden
When in doubt, try CTRL-Space
!
Although you can browse the Java 6 API directly at
the Oracle site, it is generally more convenient to open Javadoc for the class
by placing your cursor over the class name anywhere in your code and pressing
SHIFT-F2
For the classes you define, Eclipse needs to where to find their
documentation. Right click on the project name in the Package
Explorer
pane, click Properties
, select Javadoc
Location
and enter
file:/homes/iws/YourUserName/workspace331/projectName/doc/
(make sure to include file:
so the location will be recognized by
a web browser). Click OK
.
For your own or the course APIs, remember to generate the docs locally before you view them.
Ant
is a tool that can be used to automate many common tasks,
such as compiling, testing, and running code. The instructions for
Ant
are stored in a "buildfile" named build.xml
. The
buildfile specifies a set of targets
that it supports, such as
build
and test
. In our buildfile, the
usage
target lists the supported targets. One target,
validate
, is only supported on attu
.
To run Ant
, right-click (for example)
Assignments331/src/a0/build.xml
in the Package
Explorer
. Right-click Run As » Ant Build...
and in the
resulting window select the appropriate target(s).
Play with some of the elements in the Run
menu and also with
some of the run buttons on the toolbar. These will let you, for example,
select one of a number of recently run Ant
targets (or Java
programs or JUnit tests or ...) easily.
Eclipse automatically recompiles your code every time you save. Classes with
errors are marked in the Package Explorer
with red cross marks.
The compile errors, as well as compile warnings, also appear in the
Problems
view (usually situated at the bottom panel of
Eclipse).
If Eclipse says your program does not compile—but you
disagree—make sure that all of your files are saved and
compiled. If that does not work, try refreshing your project (F5
)
or using Project » Rebuild Project
to force Eclipse to recognize
the latest versions of everything.
(Minor note: the compiler that Eclipse uses is different from the standard
javac
compiler. In very rare cases, this may lead to programs
that work differently depending on which compiler is used. It's unlikely this
will happen in 331, but it's worth keeping in mind just in case.)
Right-click on the Java source file containing the main()
method and choose Run As... » Java Application
.
Again, to ease your work, look for the re-run the last (or another earlier) menu entries and/or buttons. which will re-run the last application (or JUnit test, see below) that you ran.
JUnit
is the testing framework that you will be using for
writing and running through your test cases for your problem sets. Currently,
CSE331 uses JUnit 4.
For more information, visit:
JUnit is integrated with Eclipse, so you can run tests directly. There are several ways to run tests, here is one of them:
Package
Explorer
Run » Run As » JUnit Test
If you are not working at UW CSE, you might have to explicitly add the
junit.jar
library using Project » Properties » Java Build
Path » Libraries » Add External JARs
and then telling Eclipse where your
copy of junit.jar
is located.
Here are some things that you have to do when you initially setup and run Eclipse.
/homes/iws/YourUserName/workspace331
where YourUserName
is your CSENetID username. (Don't try
"~/workspace331
" because Eclipse doesn't recognize
the '~
' character.) Mark the checkbox to make this your
default workspace.
Configure JDK. To configure Eclipse to use the Java JDK (development kit) instead of the JRE (runtime environment)
Window » Preferences
dialogJava » Installed JREs
Search...
in the
Installed JREs
paneC:\ »
Program Files » Java
and click OK
Installed JREs
list, check
the box next to jdk1.6.0_27
and click OK
Windows » Preferences
and
select Java » Compiler » Errors/Warnings
Under Generic types
, change the value of
Uncheckedgeneric type operation
to Error.