Step 4: How to build and run your project

Launch Config Create Edit Run previous step

This step will show you how to compile and run your Java program in Eclipse. Be sure to read the whole document!
Before you begin, make sure the text in your editor window is exactly the same as the text in the previous step.
  1. Go to the left Package Explorer pane. The HelloWorld folder should be opened, but if it is not, double-click on folder to open it. Then, double click on the "(default package)" to access the HelloWorld.java file. Right-click on the file and select Run As → Java Application, as in the following image:
    Run Application

    Once you run the HelloWorld program, you should notice output in the Console pane:
    HelloWorld Output
    Note that you may have to click on the tab labeled "Console" to see this output.

Now we will do one more project, which demonstrates the adding of a jar file. If you are working in a group, make sure to pass the keyboard to a different member of the group.

FortuneTeller

  1. Create a new project as you did in Step 2. Call it "FortuneTeller". However, instead of clicking Finish immediately in part 2 of Step 2, click the Next button. The following should be displayed:
    New FortuneTeller

  2. Click on the "Libraries" tab and the following should be displayed:
    New
FortuneTeller Library

  3. Click the "Add External JARs" button.
    If you are working on a CSL computer (that is, one of the computers in the CS department lab rooms), type the following path into the file chooser dialog that you get: P:\course\cs302\public\html\labs\EclipseTutorial\fortune.jar You can copy and paste this from the webpage. Then click Finish.

    If you are working on your own computer, then you won't be able to access the P: drive. Instead, download the fortune.jar file, save it on your computer, and navigate to the saved file in the file chooser dialog.

  4. When you have successfully added the fortune.jar library as shown here, click Finish.
    fortune.jar is now in list of libraries

    Note: If a red ! appears on the FortuneTeller folder that's created, this means that you have incorrectly entered the text above. If this is the case, you'll need to do the next steps to fix it:

    1. Edit the link so that it is correct in the following way: Right-click on the FortuneTeller folder in Package Explorer and choose Properties:
      FortuneTeller Properties
      The following window will appear:
      FortuneTeller Properties Resource Tab

    2. Click on the "Java Build Path" option on the left hand side, resulting in the following window:
      FortuneTeller Properties Build Tab
      You may have an error message with a red x, but the exact text for your message will differ.

    3. If you do have the red x, click on the tab labeled "Libraries", resulting in the following window:
      FortuneTeller Properties Build Libraries Tab

    4. Select the picture of the jar with the X and click the button labeled "Edit...", then try copying and pasting the above text (reminder: P:\course\cs302\public\html\labs\EclipseTutorial\fortune.jar) and click OPEN.

    5. The X should disappear. If not, try clicking the "Edit..." button and trying again. Don't proceed until the X disappears. If it doesn't work after a few tries, ask your instructor or a consultant for help.

    6. Click OK and the red X should disappear. If it did not, return to step a. above or ask your instructor or a consultant for help.

  5. Now create a new class in the FortuneTeller project called FortuneTeller, and edit it so that it looks like the following:
    FortuneTeller.java

  6. Add the file header comment, class comment, and method header comment; as you did with HelloWorld.
  7. Run the file as you did with HelloWorld (right-click and choose Run) and the following dialog box should appear (with a random fortune):
    FortuneTeller
Dialog
  8. There is a shortcut icon in the Eclipse toolbar for running the current file as an application.  Click the "Run FortuneTeller" "play" icon.
    FortuneTeller
Dialog

A last important note is that for most of your projects, you'll have more than one file that you edit. You can add more than one file to a given project the same way you added the first file to each project. However, each project will produce only one program. The different files will allow you to organize yourself in an object-oriented way. Since we made two different little programs here, we made a project for each one.


Launch Config Create Edit Run previous step


Original version created by Deb Deppeler and Sue Hazlett
Updated for Eclipse by Martin Hock
Updated for lab section use by Beck Hasti
2008-2012 Updated by Jim Skrentny
2014 Updated for v4.4 and added Editor Preferences by Deb Deppeler