My First Eclipse Experience (Spring 2007 Edition)

Added d12.15.2007: What you see here is currently a mix of the tutorial versions for Spring 2007, Fall 2007, and Spring 2008. I am in the process of updating the tutorial to be correct for Spring 2008. Until this comment disappears, you are looking at a bit of a jumble.

Note that this is currently the same as we used in Spring 2007. As of this date (August 17) it is not clear that I will have time to completely update it. For your purposes I don't think that is very important. Do what's here, then take another look at the assignment and do it.

You may go to Instructions for the second lab if you have already done everything on this page successfully, or close enough.


This is a quick tutorial on how to get started with the Eclipse Integrated Development Environment (IDE). There are many Java IDEs, each with strengths and weaknesses. One of the strengths of Eclipse is that it is free! Another is that is has a market share of more than 50%, meaning that industrial users are finding it to be a good tool: it is used more than all other Java IDEs combined.  And Eclipse is much more than just a Java IDE. Tons of plug-ins are available, making it a highly flexible and powerful tool. Example: if you are building a Web front end to a legacy (mainframe) application you are very likely going to be working with Cobol programs. No problem: get the Cobol plug-in and start modifying the legacy Cobol. The plug-in has a Cobol compiler, of course. Ditto for C++.

We will also use JCreator--free for the Learning Edition--which is fast to launch, but which lacks some of the best features of the Professional Edition. (The Professional Edition is free for a 30-day trial.) I will use it in class sometimes. It's handy if you just want to take a quick look at a program. I taught the Java version of 221 for three semesters with JCreator, before I became aware of Eclipse and before Eclipse became as polished as it is now.

Eclipse can be thought of as a Java IDE, on top of which is built IBM's WebSphere, a large collection of tools for building Web-based applications. With Eclipse you will be learning an industrial-strength tool that will also make learning WebSphere much faster if and when you get to that. IBM was a founding company of the Eclipse Foundation, which manages Eclipse. IBM is giving more and more parts of WebSphere to Eclipse; the entire package, with all updates, is now over 700MB. (Perhaps much more; I haven't downloaded all of the plugins for a year or so.) We will be using only a very small fraction of this functionality. I will give occasional demonstrations, of the "this-will-not-be-on-the-exam" type, of other things that can be done with Eclipse.

The Eclipse effort is open-source. I believe the organization has one paid employee, an executive director. All other work is done by volunteers; some key people are on loan from their employers to work full-time on the project.

This presentation is entirely tied to my section of CSc 221, Software Design Lab, at City College of New York in Spring, 2007. Even more particularly, it is the technical part of what I do in the first two lectures of the semester, one of which is in a lab. But see note above, in case I don't have time to update to the Fall 2007 HW1 assignment. The changes are minor. (Just enough to make life very difficult for anyone who might be foolish enough to turn in a friend's solution from the spring. Extremely difficult in fact. Don't do it. Penalty: zero on the homework, for all team members.)

Steps in getting Eclipse from the course CD to your hard drive

At home . . .

In the 7/118 lab you don't need to do either of these first two steps; Eclipse is installed.

  1. Insert the course CD into your CD drive. Copy the folder named eclipse (don't open it) to your C drive. (If it makes you more comfortable, create a folder named eclipse on your C drive, then copy the contents of the eclipse folder from the CD into the folder on your C drive. Does the same thing.)
  2. You can launch Eclipse from this folder by double clicking on the file name eclipse.exe. (There is no way to get the Windows Registry to handle Eclipse the way most programs are. There is also no installation, other than copying the files described above to your C drive. Why is this the case? Who knows. Personally, I doubt that it is a technical issue.)

In the lab . . .

. . . you click on the Eclipse icon on the desktop. The only place that is writable by you is C:\temp, so you will have to create a workspace (see below) there. On your own machine the workspace can be wherever you like. Make the appropriate changes in the instructions below, depending on whether you are in the lab or on your own machine. When you are done working in the lab, you can write your workspace to a CD, or email it to yourself as an attachment. The workspace contains everything necessary for you to start up at home, picking up exactly where you left off in the lab.

Back to the tutorial . . .

  1. Open Eclipse: double click on the eclipse.exe file name in the file listing in Windows Explorer. When you are asked to select a workspace, browse to C:\temp. See Figure 1 for the general appearance of this dialog, although yours will be different in detail. (When you are finished with this lab, you will be able to copy your workspace onto CD or floppy, ready to pick up at another session where you left off today. Or email it to yourself.) After selecting a workspace, wait for Eclipse to launch.

(Actually, I'd suggest you first create a folder in C:\temp ( or wherever you want on your own machine) named 221HW1 or whatever, and specify that when you start Eclipse. Before long you are going to have lots of saved workspaces; put them somewhere you can find them, and give them names that mean something to you.)

Figure 1. I have not updated all the references to the previous semester, obviously.

  1. If this is the first time Eclipse has been opened with this workspace (it is), you will get a welcome screen. Roll over the icons in the middle and make a note that there are lots of help facilities available, but we don’t want that now. See Figures 2 and 3.

Figure 2.

Figure 3.

  1. The icons on this welcome screen (which you see only when you launch Eclipse with an empty workspace) are as shown in Figure 4a, b, c, and d. Explore them if and when you wish. Not needed to get the first assignment done.

Figure 4a, b, c, d.

  1. The icon on the far right says to go to work. See Figure 5.

Figure 5.

  1. After clicking on the arrow-like thing in the far right or the X in the top left, you will see a blank Eclipse workbench. See Figure 6. Your screen layout may be slightly different. Not a problem. You may or may not see an Outline View on the far right. If you do, click on the X in its title bar is dismiss it. We don't need it for this assignment.

Figure 6. I am aware that you can't read the text on this figure. I want to limit the file size and therefore the download time. These screen grabs are here, in some cases, just so you will know that you are in the right place. If you want to read the text, look at your monitor!

  1. (I think you won't need it for the first homework, but at some point do this. Click on Window, then Preferences, then Java, then Compiler. There is a pulldown list for Compiler Compliance Level. If that says 6.0 dismiss all this and carry on. If not, pull down to 6.0 and do what they say to do.) See Figure 7. (The issue here is which version of the JDK [Java Development Kit] to use. Version 6.0 is the latest as of this writing (December, 2007), and there is no reason not to use it.  If 6.0 is not one of options, then you don't have the latest version of the JDK loaded on your machine. That will be OK for this assignment I think, but plan to download 6.0 from Sun soon.

Figure 7.

  1. Create a project: File/New/Project, or click on the New icon just below the File button and select Java Project. (Ignore the "type filter text" thing.) Click on Next. Name it Lab1, or anything else you please. Leave all selections as they are, and click Finish. (You may be asked if you want to switch to a Java perspective. If so, say yes.) See Figures 8 and 9.)

Figure 8.

Figure 9.

  1. In the Package Explorer area on the left, your new project will appear. Click on the + to expand the view. You will see that some libraries have been made available automatically. Click on the + if you like, to see what all has been made available. But we don't do much about that in the course, and nothing right now.
  2. Right click on the Lab1 project symbol and choose New/Package. Name it hw1, or anything else you please. If you start with an uppercase letter you will be told that the convention is for package names to begin with a lowercase letter. This is only a convention, but why not do it the standard way? It's the Best Practice standard. See Figures 10 and 11..

Figure 10.

Figure 11.

  1. Right click on the package name and select New/Class. Name it MyFirstEclipse, or whatever you please. Unclick Inherited Abstract Methods, and click public static void main (String args[]). The only modifier you want is public. Click Finish. When you are ready to click Finish, the dialog should look like Figure 12.

Figure 12.

  1. (If you are like me, you will have trouble remembering, at the beginning, which comes first: Project or Package? There are four things you need; try memorizing the phrase "Workspace ProPack Class." Crazy? Worked for me.)
  2. The editor window will show a basic Java program with a main method. Your workbench will look like Figure 13. Experiment with dragging the boundaries between panes on this window to make the panes larger or smaller. The comments that begin with /** are Javadoc comments. We will talk about these later. For now, you can either ignore them or erase them. What you see will not necessarily exactly match what is shown in Figure 13.

Figure 13.

  1. Modify

public static void main(String[] args) {
     // TODO Auto-generated method stub
 

}   

to read:

public static void main(String[] args) {

    System.out.println("Hello, Eclipse World!");

}

 

(If the string "Hello . . .." extends over two lines in your browser, be aware that in the program as I ran it, that didn't happen. Strings in Java cannot be broken across lines.)

  1. Click Window/Show View/Console. A console window will open at the bottom, with other views available on tabs. (If a Console tab is already there, just click on it; if the window is already open, do nothing.)
  2. If no funny red marks show in the left margin, your program is syntactically correct: Eclipse has been compiling it as you type. Make that official by doing a Save (Control+S is faster). If a red circle with an X in it appears, click on the Problems tab at the bottom to see what the problem(s) is/are. In fact, make some deliberate errors so you can see this. Then fix it and save again. Click on the Console tab. (Don't look for a Compile button. There isn't one!)
  1. Click Run/Run as/Java Application, and your output will appear in the Console window. See Figure 14.

Figure 14.

  1. Make some small change and press Control + F11, which means "Run the last thing I ran." Or click on the icon near the top left that looks like this: .
  2. If you try to run without saving, a dialog box will appear asking you what to save. You can just click OK. Or, dismiss the dialog, Save, and say to run again.
  3. Take out the statement to print Hello Eclipse etc.
  4. Insert a declaration at the class level and within main and add statements as follows. (We’ll worry later about that static, and don’t worry about the indentation differences. ) Fix any syntactic errors, Save, and run. The sum of the first ten integers is 55. (I’ll bet you knew that.)

    public class MyFirstEclipse {

        static int sum;

        public static void main(String[] args) {

            for (int i = 1; i <= 10; i++)

                sum += i;

            System.out.print("The sum of the first 10 integers is: " + sum);

        }

    }

    (See comment above about the System.out.println statement. Strings cannot be broken across lines.)

  1. Now let’s move to HW1 (homework 1). From the course CD, open the folder HW1Fall2006 and copy the file HW1StarterKit.java to C:\temp. (Or wherever, on your machine.)
  2. Back in Eclipse, right click on the the package name, hw1, and pull down to Delete. You will be asked to confirm this. Then right click on the project name, Lab1, and pull down to New Package. Name it hw1 . (Ouch. I should not have used hw1 twice this way. Perhaps I'll have time to fix this. It's not an error, exactly, but it could be confusing for newbies to Eclipse. Sorry.)
  3. Right click on the hw1 package and pull down to Import. You will see a dialog like Fig. 14.


    Figure 14.
     
  4. Click on the + to the left of General. You will see a dialog box like Figure 15. Ignore the "type filter text."

Figure 15.

  1. Select File System and browse to C:\temp (or wherever). Click Next. NOTE: you browse only as far as the folder that holds your file, not to the file itself. Your dialog box will look something like Figure 16, except your directory structure will, of course, be different.



    Figure 16.
     
  2. After you click OK you will see something like Fig. 17. Your HW1StarterKit.java file, is there, but a box must be checked for anything to be imported. Check that box and click Finish. (The presence of the .class files means that I had compiled the program. It doesn't matter whether you import these or not; if you do, they will be overwritten when you compile in Eclipse. The files named HW1StarterKit$1.class etc. are anonymous inner classes. We'll get to this idea later. Forget about it for now.)



  3. Figure 17.
     
  4. Click on the + to the left of your hw1 project name. Your HW1StarterKit.java class is there. See Figure 18.

Figure 18.

  1. Double click on its symbol to open it in the editor window. See Figure 19. If you don't see all the comments, it's because they have been collapsed into one line, as indicated by the + in the left margin. Click on that to expand the comment, which will change the +  to a -. Click on the minus to collapse, and so on.

Figure 19.

  1. If you didn't name your package hw1, you will get a nasty message saying that the package isn’t what Eclipse expected. Right click on the red error indicator at the top and select Quick Fix. (Eclipse knows what to do; it just wants you to admit how smart it is. Well, not really; you might not want the automatic correction.) Click on Add package declaration . . . An import with the package name is inserted.
  2. Click on the + in a circle, to the left of the import line. The + in this position means that some lines have been collapsed for readability. The + changes to a - and the line expands to a number of lines. Click on the - again to collapse them. Similarly in various other places.
  3. Save the file to formalize the compilation (which has already been done). (Hmmm. That may be oversimplifying things. Stay tuned.)
  4. Do Run/Run as/Java Application. The GUI created by the program will appear. See Figure 20. To be able to see the Eclipse window and the running application, I reduced the size of everything about the Eclipse window. There is no need for you to do this. In fact, the application window will sit "on top" of the Eclipse window--so long as you don't click anywhere outside the application window. The purpose of the application is to enter rudimentary data about the user in any application, for instance an on-line ordering system. For HW1 you enhance the capabilities of this program.

Figure 20.

That's it for this lesson. It's enough for one lab session. Specifically, we have not discussed the Java concepts at all, nor did we examine the HW1 program.

Whenever you are ready to leave the lab, close Eclipse and send yourself and your partner email with your Java file attached. Or copy to USB memory, or burn a CD. But email is simplest. You don't actually want to mail the complete workspace, which includes a lot of System Library files. But you could burn a CD with it, or copy to a USB drive.

To continue, go to Instructions for the second lab.

Back to the top of the Spring 2007 CSc 221 page

Back to Dan McCracken's Home Page

The only brand of chewing gum permitted in my classroom: