The main purpose of today's lab will be to get you familiar with the Eclipse IDE. Eclipse is an incredibly powerful programming environment, and once you get used to it you may even have trouble living without some of its features.
If you would like to work on programming assignments on your own machines, and would like to use Eclipse, you can download it for free at eclipse.org.
If you have used Eclipse before, you can skim quickly through this page. If you have used Eclipse, but not the Eclispe debugger, you may want to skip directly to that section.
Step Two
Select a directory for your Eclipse "workspace". Eclipse will store prefernces relating to your projects here. This will also the be the default location for source files for your projects. As such, the directory should be in your space on the H:\ drive. I chose "H:\ecl_workspace" as my workspace directory. I also elected to have Eclipse always use this directory and never ask me again.
Step Three
Once Eclipse has loaded, click the button on the far right. This button will take you to the "workbench."
Step Four
Now begin a "new project." This is a step that you will have to do several times this semester. The option to begin a new project is in the File menu, under
This will bring up a dialog which asks you to choose a "project wizard." Choose "Java Project," which is the first option, and press "Next >". This will bring up a dialog which asks you to choose a name for the project. Enter "Hello World". You can (and should) leave the rest of the rest of the options with their default values. Press "Finish" when you are done.
Step Five
Now create a new class file. This is a step that you will have to do many times this semester. The option to begin a new class file is in the File menu, under
This will bring up a dialog which asks you to choose a name for the class.
Enter "HelloWorld"
You can also choose the option to include a "stub" for the main method.
Obviously, you won't always choose this option whenever you create a new class. This time, however, it's a good idea.
Also, you can ignore the warning which says "The use of the default package is discouraged."
Since the code we will be providing for the first lab uses the default package, so can you.
Step Six
Now add the following very simple code to your main method:
int times = 1;
if(args.length > 0)
times = Integer.parseInt(args[0]);
for(int i = 0; i < times; i++)
System.out.println("Hello, again.");
As you can see, this is a simple class which takes one optional argument and prints out "Hello, again." as many times as specified by the argument (or once, if there is no argument).
Step Seven: Running your code
Find the "Run" button, which looks like a green circle with a triangle inside of it. It should be in the upper left, next to a button that looks like a bug. Press the "Run" button.
The first time you do this for a given project, you will be presented with a dialog to "Create, manage, and run configurations."
A "run configuration" is a way of managing the options associated with executing code. In this instance, we want to double-click "Java Application" in the list on the left. This will give us a new dialog which allows us to change a lot of settings. For now, just press "Run" in the lower right.
Now you should be returned to the main workbench window, and you should see that the code has executed. In particular, the botton of the workbench should now be occupied by a "Console" box, where the program has printed "Hello, again." just once.
Step Eight: Command line arguments
Now let's pass in a command line argument to the program, to make it print out "Hello, again." more than once.
Find the "Run" button once again. This time, press the down arrow to the right of the green circle, and select "Run..." from the drop-down menu. This should return you to the "Create, manage, and run configurations" menu. Find the "Arguments" tab, and click it. Then enter some number in the "Program Arguments" field.
Now press "Run" in the lower right. This will have the same effect as if you had been at a command line and entered
$java HelloWorld 5You should again be taken back to the workbench, where the console should show that "Hello again" has printed the number of times you specified.
Step Nine: Debugging
Now we want to introduce you to Eclipse's powerful debugger.
As a simple introduction, we're going to watch the value of the iterator variable going up from 0 to the value you entered in the "command line arguments."
The first thing we have to do is set a breakpoint.
There are two ways of doing this. One is via the menus. First select the line of code which contains the for loop. Then navigate the menus and choose .
The second method is to right-click in the black bar to the left of the code, also in the line with the for loop, and choose "Toggle breakpoint."
Either way, a blue dot should appear in the black bar to the left of the code at the relevant line.
Now click the button which looks like a bug. This button is to the left of the "Run" button we used earlier, and is the "debug" button. You will be asked if you want to go into the "debugging perspective." Click the box to "Remember my decision," and then click "OK."
Now your view will change, so that you can see information relevant for debugging.
In the top-right, you will have a window which displays the values of all of variables
which have been declared so far in this execution of the class
whose code is displayed.
For us right now, this will be the argument args and the variable times.
You should also see that the line with the breakpoint is highlighted in green. This indicates that the debugger has halted before this line, and is waiting for you to inspect your code and tell it to advance.
At this point, you have a lot of options. Most important, you can "step into", "step over," and "resume." Each of these advances the code by a certain amount.
- Step into applies when the breakpoint is at a method call. This advances the breakpoint to the beginning of the method, and takes you to the code for that method. This allows you to "step into" the method and watch its exectuion. If the breakpoint is not before a method call, step into advances the breakpoint by one line.
- Step over also applies when the breakpoint is before a method call. Step over allows the method to execute, and breaks again at the next line after the method call. When the breakpoint is not before a method call, this also advances the execution by one line.
- Resume resumes execution and runs all the way to the next breakpoint
You should try all three of these, and see how the value of i first appears in the "Variables" window, and then increases over iterations.