CSE 341 -- Java and Eclipse

Contents:


Java Compiler and Runtime

The latest version of Java as of January 2003 (1.4.1) is installed on the instructional Windows machines. Use this one also if you are working on your own machine - it's free. The Sun Java download is available for Windows and Linux (there must also be a Java for Macintosh somewhere).


Eclipse

For 341 we recommend using the Eclipse programming environment. If possible, use the latest stable build (M4 stable build of 18 Dec 2002). Unfortunately the lab has an older version installed on the lab machines, but except for the lack of a built-in Applet Viewer it shouldn't be that different.

Eclipse is also free, and you can also download it for use on a personal machine. Eclipse is available for Windows, Linux and Macintosh.

Eclipse comes with a tutorial - pick the "Help" menu item on the top bar, then "Help Contents". Start with the Workbench User Guide/Getting Started/Basic Tutorial. Then go through the Java Development User Guide. (Skip the team tutorial for now - that will be relevant when you are doing group projects later.)

You can also read the tutorial on the web at http://dev.eclipse.org:8080/help/.

In the "Preparing the Workbench" section of the Java tutorial, the tutorial requests that you download an examples file. If you're working in the CSE lab, somebody may already have done this - if not, find it from the download page for the version of Eclipse you are running (M4 stable build of Wed, 18 Dec 2002, for the Lab machines). If the file d:\eclipse\plugins\org.eclipse.jdt.ui.examples.projects\archive\junit\junit37src.jar exists, someone already did get the download.

If you're using the Lab machines, you'll need to use the "export" command to save your work on a server, so that you can reload it into Eclipse if you then work on another machine. Note that the next person that logs into the machine will see your work in Eclipse unless you delete it before logging out.

There is also documentation on the Eclipse FAQ page. In particular, to get a quick start on using Eclipse for Java, see How do I write a simple "Hello World" program?.

Checklist of Things to Know about using Eclipse

Optional but Useful:

Essential Later:

Using cvs with Eclipse

There is lots of documentation on this in the eclipse help system, and in the web page version at http://dev.eclipse.org:8080/help/.

Here are some quick-start directions for getting the Java example files used in class. You can always get these directly - we'll put them on the web pages as .java or .zip files. But it's easier to do this with cvs. The cvs repository in these directions is read-only for you - later, when you're working on class projects, we'll get writeable repositories set up.

Set up a connection to the server. In eclipse, select the cvs perspective from Window/Open perspective. Right-click in the cvs repositories pane, select new/repository location. Fill this in as follows:

Host: ceylon.cs.washington.edu
Repository path: /projects/instr/03wi/cse341/examples
User: [your user name]
Password: [your password on the instructional linux machines]
Connection type: extssh

Click on the plus sign next to the disk icon, then click on the plus next to HEAD. Left-click on 341-sample-code. Right-click, and pick Check Out as Project. That should download all the sample code to your machine.

Switch to the Java perspective, and open up the 341-sample-code package. The code should all be there, compiled, and ready to run. This project has a local copy of JUnit, so that you don't need to add it as an external JAR. (JAR stands for Java Archive - it's a file with compiled Java code that the system can link in.)

The code on your machine is a local copy - you can modify it and experiment as you wish without affecting the repository copy. If you ever want to get a clean copy again from the repository, or if there is an updated version that you want to get, do the following: in the Java Perspective, right click on the 341-sample-code project. Select Replace with, then Latest From Repository. (This will overwrite any local changes you made. The Team menu item provides lots of other options for integrating changes from the repository, etc - you shouldn't need any of that at this point, but if you're interested see the Eclipse tutorial.)

Important: eclipse will leave your user ID and password associated with this repository on your machine. If you are working on your own machine, that's what you want. If you're working in the lab, in the Java perspective right-click on the 341-sample-code, project, pick Team/Disconnect, and disconnect it from the repository. Then go to the cvs perspective, pick the repository, and pick Discard Location.


JUnit

JUnit is a nifty framework for writing tests. These are automated, scriptable tests that you can run repeatedly. They are well-supported by Eclipse -- in the Eclipse tutorial, go to Java Development User Guide, then Tasks, then Using JUnit.

A project needs to include the JUnit JAR to use JUnit. An easy way to handle this is to check out "341-sample-code" using cvs. This project already has a local copy of JUnit, so the examples will run right off. Also, if you write any additional classes in this project, the new classes will have access to JUnit as well.

Otherwise, here's how to add JUnit as an external JAR if you're creating a new project. See the directions in the Eclipse tutorial for more details.

  1. Right click on the project name, and select "properties".
  2. Pick the "libraries" tab.
  3. Pick "Add External JARs".
  4. Navigate to the directory that contains JUnit JAR - it's eclipse\plugins\org.junit_3.8.1
  5. Pick the junit JAR and click OK.

If you want to debug JUnit code, or a method being tested with JUnit, you need to set a breakpoint to get the JUnit code to halt. We'd suggest setting the breakpoint at the beginning of the test method, rather than in the method being tested. That lets you check that everything in the test framework is set up properly. Then single step away - be sure and use the "step into" command rather than the "step over" command when you get to the method you are testing, so that you step into its code. Once you've set an appropriate breakpoint, pick the downarrow next to the bug icon, pick Debug as JUnit Test, and you're off.

For general information JUnit and its philosophy, see http://www.junit.org.