My First Eclipse Experience. (Fall 2008 Edition)

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 plugins are available, making it a highly flexible and powerful tool.

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. Some people like NetBeans. You will not be tested on  your knowledge of Eclipse, so I suppose you can use Notepad and command line if you wish. Most students rapidly see the wisdom of using what others are using.

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. By learning Eclipse you will be learning something of industrial strength 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 rather rapidly giving more parts of WebSphere to Eclipse; the entire package, with all updates, is now over 700MB. 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 Fall, 2008. Even more particularly, it is the technical part of what I do in the first lecture of the semester.

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

At home . . .

  1. Create a folder named eclipse on your C drive. (In the 7/118 lab you don't need to do any of  these first four steps; Eclipse is installed.)
  2. Point your Web browser at: http://www.eclipse.org/downloads/  Click on the link Eclipse IDE for Java Developers (85 MB) and choose your operating system (usually Windows). Choose a mirror site; Columbia is nearby, but any other http site will do.
  3. You will be asked whether you want to Open or Save the download; say Save. You will be asked where; navigate to the folder you created in Step 1. Go to that folder, open it, and right click on the filename eclipse-java-ganymede-win32.zip.  Pull down to Extract to Here.  That will create another folder named eclipse containing the unzipped files and folders.
  4. You can launch Eclipse from this folder by double clicking on the file name eclipse.exe. This process will not put Eclipse into the Windows Registry.
     

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, you can write your workspace to a CD or thumbdrive, or email it to yourself as an attachment. IF DO NOT DO THIS YOUR WORK WILL BE LOST!

Back to the tutorial . . .

  1. Open Eclipse: double click on the eclipse.exe file name. When you are asked to select a workspace, browse to C:\temp and create a workspace (or navigate to the one you created above. (When you are finished with this lab, you will be able to copy your workspace onto CD or thumb drive, ready to pick up at another session where you left off today.) 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.)

  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  and make a note that there are lots of help facilities available, but we don’t want that now. See Figures 1 and 2a through 2e.

Figure 1.

Figure 2a, 2b, 2c, 2d, and 2e.

  1. After clicking on the arrow-like thing at the right or the X in the top left, you will see a blank Eclipse workbench. See Figure 3. (Your screen layout may be slightly different. Not a problem. If you have one or more small windows at the right, you can click on the X in their titles to dismiss them--if you wish.)

Figure 3. A blank workspace.

  1. I think you won't need this 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 1.6, dismiss all this and carry on. If not, pull down to 1.6 and do what they say to do. If you don't know which version of Java you have, Google  java version  and click on the first hit; when that page loads, click on Verify Java Version. Sun will look deep inside your machine and report the version number. If you don't have 1.6, consider getting it.
  2. Create a project: File/New/Java Project, or click on the New icon just below the File button and select Java Project. Name it Lab1, or anything else you please. Leave all checkboxes as they are. Click Finish. (You may be asked if you want to switch to a Java perspective. Say yes.)
  3. 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.
  4. Right click on the Lab1 project symbol and choose New/Package. Name it helloJava, or anything else you please, and click Finish. 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.
  5. Right click on the package name and select New/Class. Name it MyFirstEclipse, or whatever you please, and specify helloJava as the package name. Unclick Inherited Abstract Methods, and click public static void main. The only modifier you want is public. Click Finish. When you are ready to click Finish, the dialog should look like Figure 4.

Figure 4.

  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 5. 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.

Figure 5.

 

  1. Modify    

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

 
   to:

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.)
  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.
  1. Click Run/Run as/Java Application, and your output will appear in the Console window.  (If Java Application is grayed out, click in the Editor window to give it focus, and try again.)
  2. 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: . If you type as new string, as in
        System.out.println("I can program!");
    note that when you type one double quote you get two double quotes. Eclipse knows about double quotes, parentheses, curly braces, etc.
  3. 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.
  4. Take out the statement to print Hello Eclipse etc.
  5. 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);

        }

    }

    (Note: Strings cannot be broken across lines.)

     

  1. Now let’s move to HW1 (homework 1). .
  2. In Eclipse, right click on the the project name, Lab1, and pull down to  New Package. Name it hw1 .
  3. Create a new class called HW1StarterKitV2. You don't need the public static void main method this time
  4. Go to Blackboard, CSc 221, Course Documents, HW1. Copy the program.  One way to copy the whole program is to select it with the mouse. The other is to Select All (Ctrl-A), Copy (Ctrl-C), then to you Eclipse editor and Paste (Ctrl-V), after which you will have to delete the Blackboard stuff at top and bottom. A bit painful, but you'll get good at it. See Figure 6.


    Fig. 6.
     
  5. What a mess! There is no indentation! You MUST correct this; I won't even attempt to read such a program. But it's simple: Select All, then Reformat with Ctrl-Shift-F. See Figure 7.


     
  6. That's better. Note the syntax coloring: Java keywords are dark red, comments are green, and member data names are blue.
  7. 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 . . . The package name is inserted.
  8. Right click on the - in a circle, to the left of the first line. The - in this position means that some lines can be collapsed. The - changes to a +and the line collapses to one line. Similarly with the import statement. Click on the + to get the lines back in sight.
  9. Save the file to formalize the compilation (which has already been done). Don’t look for a Compile button! There isn’t one.
  10. Do Run/Run as/Java Application. The GUI created by the program will appear.
  11. Now you can get to work on HW1. Follow the instructions on my website (ccnyddm.com) at

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. If you don't do this, all your work will be lost!!! Temporary files are erased when you log out.

 

Back to Dan McCracken's  Home Page