Eclipse basics

This is a brief introduction to using Eclipse. It includes primarily the steps needed to create a project and work with it effectively.

Creating a new project

Eclipse uses a 'project' to group work. Each project can have its own build path and can be compiled separately from other projects. Projects contain packages. Projects are stored in their own directories, of which each package is a subdirectory.

Typically when creating an agent-based model you will want to keep that model as its own project. To create a project in Eclipse, you must:

  1. Make sure you are in the Java Perspective; to do this, go to the 'Window' menu, select 'Open Perspective', and choose 'Java'
  2. From the file menu selection select 'New' and choose 'Project...'
  3. Choose 'Java Project'
  4. In the dialog box, you will be prompted for a project name; choose something short and convenient.
  5. There will also be a check box, which, if checked, will place the project in the Eclipse default workspace. In practice you will more commonly want to store the project somewhere else on your hard drive, so select browse and choose the folder in which you want your source files to be stored.
  6. Click 'next.'

At this point your project will be created; you will automatically be taken to a dialog box where you can set the properties of the project; we will discuss this as a separate step because it can be done any time. Click 'Finish' to complete the creation of the new project. The new project will appear in the Package Explorer window.

Adding to the build path

One of the first things you will need to do is specify the build path for a given project. To do this, right-click on the project in the Package Explorer window, and select 'Properties.'

To add (for example) the RePast library to the build path for the newly created project, use the 'Java Build Path' option from the menu. You will need to select the 'Libraries' tab, and then select 'Add External JARs.' Browse to where you have the RePast library installed from its zip files, and navigate to \lib. RePast actually includes a number of JAR files; the one needed to run the base RePast is called simply repast.jar; select this one and click 'Open' to add it, then click 'OK' to close the dialog.

Creating class files

Creating a class file in the project is as easy as right-clicking the project in the Package Explorer window and selecting 'New...Class' from the menus.

However, before creating a new class, it is wise to give some thought to the package structure you will be building. Often agent-based models can be subsumed within only one package; it is even possible to to decline to specify any package and allow Eclipse to put everything into a 'default' package, which is what it will do if you do not create a package first. A better type of organization is to create at least one package; this make the creation of JAR files and eventual distribution of the code simpler.

One other useful strategy is to create packages as needed for the actual execution of the code, but to keep any 'test' classes outside the package structure; they can be omitted from the final distribution.

Compiling

Eclipse compiles projects when saving them; you can force a compile by selecting your project from the project window and choosing the 'Rebuild Project' option from the 'Project' menu.

Running your project

Typically Java programs are run from a command line. You can, however, run them within the Eclipse environment, and this is far more convenient. Eclipse actually lets you run programs in many ways, so many that it asks you to create a 'run configuration' and save it. This is easier than it sounds.

To run a program, go to the 'Run' menu. Select 'Run As'. Typically you will choose 'Java Application'- this will start the source file you are working in. If you choose 'Run...' you will be presented with a dialog box that is, unfortunately, not very clear. You can choose a project and a run configuration from the box at left, or select a new one from the 'main' tab at right. For each project you must specify which source file has the 'main' method you want to run for that project. You can also use the other tabs to provide arguments for your application, as you would from the command line.

Coding aids

Eclipse helps you in many ways. For example, if you type code like:

Vector V = new Vector();

but you haven't specified that the class 'Vector' should be imported (see Import), eclipse will flag it with an error symbol at the left of the editor window. Clicking it will give you a set of options; one of these is to add the required import. Eclipse has already traced through the build path, found the place where the class 'Vector' is defined, and is offering to add this to your import list.

Even if Eclipse can't figure out the action that has to be taken to fix an error it flags, it can usually tell you what the error is. This is often of immeasurable value.

Another example: suppose you specify that a new class implements an interface. At first, Eclipse will automatically flag as an error the fact that you haven't implemented the methods required by the interface (see Interfaces). If you click on the error flag, Eclipse will offer to add the required methods automatically- actually it simply provides stubs for these, because your specific implementation will be tailored to your needs. Note, however, that the stubs it creates are usually already provided with a framework for JavaDoc comments.

Lots of very helpful features are found under the 'source' menu; for example, if you have declared class level variables, Eclipse can generate getter and setter methods for the variables you specify.

The moral of the story is: let Eclipse help you.