Introduction to Eclipse 3.0
(based on Lab01 for ITK 168)
Loading, Compiling, Executing, and Debugging an Existing Program
Follow the instructions below. Refer to this section when you need to load, compile, or execute a program in later activities.
0. Copy the folder Lab01 from the T:\itk353 directory to your H:\ drive.
1. Creating a project in Eclipse Open the Java Tools folder on the desktop, and double-click on the program labeled Eclipse 3.0 work – H drive.
From the File menu, select New and then Project….
In the pop-up window that appears, make sure Java Project is selected, and click on the Next > button.
Type the name Lab1 into the Project Name textbox and click on the Finish button.
After a few moments, the following dialog box will appear.
Check the Remember my decision checkbox and Choose Yes.
2. Importing a Java file into a project
Now we want to load a Java file into our project in Eclipse. You’ll follow this procedure any time you are provide with a file that is to be part of your project. The file we’re going to import into Eclipse in called TempConverter.java. It should be on your H: drive, inside the Lab01 folder that you copied earlier in this lab exercise.
In your Eclipse window, make sure that the Lab1 project is selected. Then go to the File menu and choose Import…
In the resulting dialog box, select File System and then click on the Next button
In next dialog box, you’ll use the Browse button next to the From directory textbox to locate the folder in which the file is located. It’s very important that you’re looking for the folder the file is in, not the file itself. Although it’s confusing at first, this is actually a nice feature, because it allows you to import several Java files that are located in the same folder into a particular project.
Remember that you’re looking for the Lab01 folder on your H: drive. Once you’ve selected that folder, click the OK button.
Select TempConverter.java, and make sure that the folder you are importing into is Lab1.
Then click on the Finish button. The import process may take a little time. Once it has completed, click on the plus (+) sign next to Lab1. You will see that TempConverter.java has been imported into the project. Double-click on TempConverter.java, and the code will appear in one of the panels in the Eclipse window.
You can examine this program if you like to get a feel for what a Java program looks like. It involves event handling and GUI programming in Java.
3. Running a Java program in Eclipse
Computer programs aren’t very interesting until we make them actually run. To run a Java program in Eclipse, select the file you want to run a program from (in this case, TempConverter.java), and then go to the Run menu and select Run as and Java Application.
Alternatively, you can right-click on the file, and then select Run As and Java Application from the resulting pop-up menu.
Running this program will produce a small dialog box (see below).
4. Debugging a Java program in Eclipse
a. Create a Java class called TestDebugging.java (File | New | Class).
b. Click Finish.
c. Replace the class with the following code
public class TestDebugging {
public static void main(String[] args) {
String array1[] = new String[5];
for (int i = 1; i < array1.length(); i++)
array1[i] = "Name "+i;
for (int j = 0; j < array1.length; i++)
if (array1[j].startsWith("Na"))
System.out.println("'"+array1[j]+"' starts with 'Na'");
} // end of main
} // end of class
d. Examine the program and observe that there are two compilation errors. (Note that the compiler is always running behind the scene. Thus, there is no "Compile" option in this environment to choose from.) Can you fix the errors? Hover your cursor over the errors (squiggly pink lines) to reveal the error messages.
e. To fix the errors: Change the 1st length() to length and change i in the 2nd for loop to j.
f. Save the code and you will see that the errors are gone!
g. Now run the program (right click on the TestDebugging.java and choose Run | Java Application). Note the NullPointerException produced!
h. The 2nd line of the error message on the Console shows that there is a NullPointerException at line 22 of the program. You can also click on this line to bring you directly to that portion of the program.
i. Let’s trace the program to see why the runtime error occurs by placing a breakpoint on the 1st for-loop. You can do this by double clicking on the left stripe of the editor window. (Double click again to remove the break point.)
j. Click on the Debug icon on the toolbar. You will get this popup window to switch to the Debug perspective. Check the Remember my decision checkbox and Click Yes.
k. Now you are put on the Debug perspective and the 1st line of the program is ready to be executed.
l. You can step through your program to debug the program from this perspective. Click on the Step Over (this one). Click on the ‘+’ sign in front of the variable array1. Go on clicking Step Over and you will see the variables being initialized to string values and you will find that the array is not properly initialized. A quick glance at the window reveals that the 0th element of the array was not properly initialized. Thus, when executed, the NullPointerException is thrown. You can continue stepping over the program to see this error.
m. To fix the errors: change the value of 1 to zero in the initialization of the 1st loop. Everything should be fine now :-)
n. Run the program by clicking on the Run button. You will get the output: