laboratory 0: - university of virginia school of …cs201/spring2005/labs/lab1/eclipse.doc · web...

22
CS 201—Software Development Methods Fall 2004 Tutorial #1 Eclipse

Upload: buituyen

Post on 08-Jul-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Laboratory 0: - University of Virginia School of …cs201/Spring2005/labs/lab1/eclipse.doc · Web viewFor syntax problems, Eclipse uses a combination of underlining in red (ala Microsoft

CS 201—Software Development MethodsFall 2004

Tutorial #1

Eclipse

Page 2: Laboratory 0: - University of Virginia School of …cs201/Spring2005/labs/lab1/eclipse.doc · Web viewFor syntax problems, Eclipse uses a combination of underlining in red (ala Microsoft

Laboratory #1 CS 201—Software Development Methods Fall2004

ECLIPSE 3.0 DEVELOPING A SIMPLE PROGRAM

This tutorial is meant to serve as a step-by-step walkthrough of the process of developing projects – including creating/opening, building, and executing – in the Eclipse 3.0 Integrated Development Environment (IDE).

In 2001, IBM donated $40 million dollars toward the creation of an open-source Java development suite called Eclipse for use with large-scale software projects. The design of Eclipse allows users to easily install custom tools known as plug-ins to complement its functionality. Today, a number of plug-ins are available to assist in the software development process. Eclipse includes a debugger, compiler and jar creator. For the purposes of the class we will use Eclipse 3.0 with the J2SDK 1.4.

Note: Completing this walkthrough requires Eclipse 3.0 to be installed on the machine on which you will be working. All computers in Stacks in Thornton Hall and in the CS 201 laboratory (Olsson Hall 001) have this software installed.

Last Change: May 9, 2023 Page 2 of 14

Page 3: Laboratory 0: - University of Virginia School of …cs201/Spring2005/labs/lab1/eclipse.doc · Web viewFor syntax problems, Eclipse uses a combination of underlining in red (ala Microsoft

Laboratory #1 CS 201—Software Development Methods Fall2004

Definitions and Terminology

Project A collection of source code, class files and any accompanying items (jars, images, sounds, etc.).

Perspective A view that allows a set of windows and information about open projects to be displayed.

Window A GUI object that can contain other GUI objects.Workbench The Java editor in Eclipse.Run Making the class files perform their programmed logic.Debug Walking through the code to find errors.Project Explorer Lists the projects that are currently open, and allows perusal of the contents

of each project.Outline Lists the methods, static variables, etc. in the currently open class, clicking

on one of these sends the workbench to the definition.Console Displays the output of a program that was Run.Problems Lists the compilation issues in a table form; double-clicking a problem

displays the location of the error.Tasks Lists tasks that have been added to the source file, i.e. TODO, FIXME, etc.

Last Change: May 9, 2023 Page 3 of 14

Page 4: Laboratory 0: - University of Virginia School of …cs201/Spring2005/labs/lab1/eclipse.doc · Web viewFor syntax problems, Eclipse uses a combination of underlining in red (ala Microsoft

Laboratory #1 CS 201—Software Development Methods Fall2004

Part 0: Starting Eclipse

To open Eclipse 3.0 from an ITC lab, choose Start >> S.E.A.S. >> Java Apps >> Eclipse >> Eclipse. In Olsson 001, you should just be able to double click the Eclipse icon.

When you first open Eclipse you are greeted with the Welcome splash screen; clicking the Go To Workbench button (upper right corner) takes you to the workbench.

Last Change: May 9, 2023 Page 4 of 14

Page 5: Laboratory 0: - University of Virginia School of …cs201/Spring2005/labs/lab1/eclipse.doc · Web viewFor syntax problems, Eclipse uses a combination of underlining in red (ala Microsoft

Laboratory #1 CS 201—Software Development Methods Fall2004

Last Change: May 9, 2023 Page 5 of 14

Page 6: Laboratory 0: - University of Virginia School of …cs201/Spring2005/labs/lab1/eclipse.doc · Web viewFor syntax problems, Eclipse uses a combination of underlining in red (ala Microsoft

Laboratory #1 CS 201—Software Development Methods Fall2004

The first thing you will want to do is switch the workbench over to the Java Perspective. Do this by Window >> Open Perspective >> Java.

Another perspective you will use is the Debug perspective. Switch to it by Window >> Open Perspective >> Debug. Not much to see here at this point, so go back to the Java Perspective.

The last point of the Workbench that we shall discuss is the Preferences dialog box, accessed via Window >> Preferences. You will not change any settings now, but you can later change auto-completion, editor appearance, code formatting, and other settings from this window. Preferences can be saved, which is particularly useful in public labs, by clicking Export. The next time you use Eclipse, you can Import your epf (Eclipse preferences file).

Last Change: May 9, 2023 Page 6 of 14

Page 7: Laboratory 0: - University of Virginia School of …cs201/Spring2005/labs/lab1/eclipse.doc · Web viewFor syntax problems, Eclipse uses a combination of underlining in red (ala Microsoft

Laboratory #1 CS 201—Software Development Methods Fall2004

Part I: Creating a “Hello World” Jar

1. Choose File >> New >> Project, and choose Java Project. Click Next.

2. In New Java Project dialog box enter “HelloWorld” for the Project name. Select Create project at external location and use a path in your home directory. If you do not do this, you may lose your work! Select Create separate source and output folders (this often makes code management easier).

3. Click Finish (Next would allow you to customize the output folders, project dependencies, libraries, and build/export order, but you do not need to do that).

4. Now you have successfully created our first Java Project and need to add classes. To do this, right-click on the project and choose New >> Class.

Last Change: May 9, 2023 Page 7 of 14

Page 8: Laboratory 0: - University of Virginia School of …cs201/Spring2005/labs/lab1/eclipse.doc · Web viewFor syntax problems, Eclipse uses a combination of underlining in red (ala Microsoft

Laboratory #1 CS 201—Software Development Methods Fall2004

5. This brings up the New Java Class dialog box. Enter “cs201.lab1” as the Package and “HelloWorld” as the Name. Check public static void main(String[] args). Note that we do not use the default package and instead create a custom package path. This is highly recommended as it helps keep the code more logically separated.

6. Clicking Finish creates the HelloWorld.java file with a main method.

7. Eclipse has a number of useful features to assist developers; one is auto-completion of code. For example, if you type the name of an Eclipse-recognized Java class followed by a “.”, Eclipse will open a popup-menu that lists the methods/variables associated with the class. The list is narrowed as you type more. Once a single item is highlighted, Eclipse shows the JavaDoc for that method/variable. Inside the public static void main class, type “System.”. A list of all of java.lang.System’s static methods/classes/variables appears. On typing o, out is the only possible option. Hit enter, and Eclipse writes the rest of out for you.

Last Change: May 9, 2023 Page 8 of 14

Page 9: Laboratory 0: - University of Virginia School of …cs201/Spring2005/labs/lab1/eclipse.doc · Web viewFor syntax problems, Eclipse uses a combination of underlining in red (ala Microsoft

Laboratory #1 CS 201—Software Development Methods Fall2004

8. Complete the line of code so it reads System.out.println(“Hello Eclipse!”); and select File >> Save.

9. It is now decided that the class name HelloWorld is not as appropriate as HelloEclipse. We’ll use this opportunity to refactor our code. To change the name, right-click on the class name in the Package Explorer window and choose Refactor >> Rename.

10. This brings up the Rename Compilation Unit dialog box. Change the name to HelloEclipse and be sure that Update references is checked. It is also a good idea to check the other boxes as well. These options ensure that all references to your class name are corrected for you. No need to go back through your all code to change every occurrence of the class name. Click OK >> Continue. (don’t worry about the warning that comes up).

Last Change: May 9, 2023 Page 9 of 14

Page 10: Laboratory 0: - University of Virginia School of …cs201/Spring2005/labs/lab1/eclipse.doc · Web viewFor syntax problems, Eclipse uses a combination of underlining in red (ala Microsoft

Laboratory #1 CS 201—Software Development Methods Fall2004

11. Now you are ready to run your program. On the menu bar, choose Run >> Run As >> Java Application. In the console window, you now see the output of our little application. (If we had not included a main class Eclipse would popup a dialog box telling us that the active class contains no main type or class depending on the method of execution). If the Console does not automatically come up, choose Console from the file bar of the bottom window.

12. Now that we have a complete class, we can export it as a jar for easy use. To do this, we will use Eclipse’s built in jar export utility. Choose File >> Export. In the Export dialog box choose JAR File and click Next. Now you should see the JAR Export dialog box. If the checkbox next to HelloWorld is gray simply uncheck it and then recheck it. In the window on the right, check .classpath, .project, and Export java source files and resources (the first two are in the upper right window). Enter HelloEclipse.jar as the JAR file and click Next.

Last Change: May 9, 2023 Page 10 of 14

Page 11: Laboratory 0: - University of Virginia School of …cs201/Spring2005/labs/lab1/eclipse.doc · Web viewFor syntax problems, Eclipse uses a combination of underlining in red (ala Microsoft

Laboratory #1 CS 201—Software Development Methods Fall2004

13. On the next dialog box, accept the defaults and click Next. Finally on the third dialog box choose the cs201.lab1.HelloEclipse class as the main class and click Finish. This creates an executable jar file that will simply print out “Hello Eclipse!” when run.

Last Change: May 9, 2023 Page 11 of 14

Page 12: Laboratory 0: - University of Virginia School of …cs201/Spring2005/labs/lab1/eclipse.doc · Web viewFor syntax problems, Eclipse uses a combination of underlining in red (ala Microsoft

Laboratory #1 CS 201—Software Development Methods Fall2004

Part II: Debugging

So far we have not discussed issues that occur in developing code, namely the two types of problems: syntax and programmatic. Eclipse helps immensely with both of these issues. For syntax problems, Eclipse uses a combination of underlining in red (ala Microsoft Word) and putting icons on both sides of the text editor indicating the positions of the errors (the right side shows the relative position of an error in the active document). Hovering over either side gives a description of the error, and clicking a red box on the right side brings the error to screen and highlights the offending code. For programmatic problems, Eclipse has a Problems window that lists all compilation errors as well as a Debug Perspective that allows one to step through code, insert breakpoints, and watch local variables using the toString method.

Create Problematic class:1. If it is not already open, reopen your HelloEclipse.java file.

2. Now add a method getOutput() that is static and returns a String as shown and change your main() method to use it. After making these changes, save your file.

3. As you can see, there are two errors, and Eclipse points them out in numerous ways. To correct the first error, left click on the light bulb with the X over it to the left of the BigInteger… line. Select Import ‘BigInteger’ (java.math). When you save your file, these red lines should go away.

4. Repeat this step for the other error, selecting Change to ‘output’. (remember to save again)

5. Now Eclipse shows no errors, so we can run the main method shown above (Run >> Run As >> Java Application). The output is “Hello Eclipse, from CS202”… But you’re in CS201 now, not 202. The error may be obvious to you, but let’s step through the code.

Last Change: May 9, 2023 Page 12 of 14

Page 13: Laboratory 0: - University of Virginia School of …cs201/Spring2005/labs/lab1/eclipse.doc · Web viewFor syntax problems, Eclipse uses a combination of underlining in red (ala Microsoft

Laboratory #1 CS 201—Software Development Methods Fall2004

6. Double-click on the grey bar to the left of the line starting with BigInteger… to add a breakpoint. When debugging, Eclipse will run to here and await further instructions.

7. Choose Run >> Debug As >> Java Application.

8. You will be asked to switch to the Debug Perspective. Select Yes. The screen will now look very different.

9. Looking at the Variables window, we see a list of all local, initialized variables (num). Clicking on num gives the toString of it in the Details (bottom) section of the window.

Last Change: May 9, 2023 Page 13 of 14

Page 14: Laboratory 0: - University of Virginia School of …cs201/Spring2005/labs/lab1/eclipse.doc · Web viewFor syntax problems, Eclipse uses a combination of underlining in red (ala Microsoft

Laboratory #1 CS 201—Software Development Methods Fall2004

10. Pressing F6 allows you to step line-by-line over your code and any method calls (F5 allows you to walk through each method, but that will not be necessary here). After hitting F6 once, classNum comes up in the variables window. Select it, and you will see that the value is 202, so the error must have occurred on instantiation of classNum.

11. Try removing the + 1 in BigInteger.valueOf(num + 1) and click Save. We return to the original debug line. Repeat step 10, and the value of classNum should now be 201.

12. Terminate the debug session by selecting Run >> Terminate.

13. Return to the Java Perspective and run your main method. The output should now be correct.

We have gone over the basics of debugging, but there are two more functions that will be very useful in debugging applications: Run >> Resume (F8) and Run >> Run to Line. The former allows you to let the application run until it hits another breakpoint (at which point you can examine it again). The latter allows you to run to the line that the cursor is on without inserting breakpoints.

Conclusion

Congratulations! You have now have experience: Starting Eclipse Creating projects

and classes Refactoring class

names Running classes Creating jar files Debugging both

syntax and logic errors

Last Change: May 9, 2023 Page 14 of 14