SIMS 255: Lab 3

Thursday, September 18, 2003

Using the Eclipse IDE and other Java development tools

IDE stands for Integrated Development Environment. The purpose of an IDE is to help you write, run and debug programs. Today we will use the open source Eclipse IDE (http://www.eclipse.org) to:

We will also have a brief introduction to the Java development tools that come with the Java JDK, and TextPad.

Why use an IDE?

Hello World: an introduction to the IDE.

Let's write a simple program to make sure everything's set up correctly as a warm-up.

  1. To launch the Eclipse IDE, choose Start > Programs > Software Development > Eclipse 2.0 from the Windows Start menu.

    NOTE: always start Eclipse from the Start menu (rather than by navigating to it using Windows Explorer or some other method). Your Start menu shortcut is set up so that Eclipse will look to your H:\ (home) drive for projects and source files. Without this, your projects and source files will get saved to the C:\ drive of the PC you're working on, making it difficult to access your work from different machines in the lab.

  2. You'll probably want to maximize Eclipse to take advantage of as much of the screen space as possible.
  3. Choose File > New > Project...
  4. Select Java and Java Project, and click Next.

  5. Type HelloWorld in the Project name field, and click Finish. (NOTE: "HelloWorld" is one word. No space.)

    The project will automatically be saved in your home directory, under H:\eclipse\workspace\HelloWorld. (You can change the default directory at some point if you wish, but for the purposes of this lab session, just leave it on the default.)

  6. In the Package Explorer window, highlight the HelloWorld project.
  7. Choose File > New > Class...
  8. Type HelloWorld in the Name field.
  9. Check the public static void main(String[] args) box, and click Finish.

  10. You should now have a file titled HelloWorld.java within the HelloWorld project folder, and the file should have automatically opened.
  11. Insert this line within the main method (type it exactly as below):

    System.out.println("Hello World!");

  12. Choose File > Save HelloWorld.java
  13. Choose Run > Run As > Java Application

    If Java Application is not available, try these alternate steps:

    a. Choose Run...
    b. Select Java Application
    c. Click the New button
    d. Clicked the Run button

    The Console window at bottom shows "Hello World!"

    Ta da! You have just created, compiled, and run your first Java application using the Eclipse IDE.

Let's look at the files that Eclipse created.

In Windows, navigate to

   h:\eclipse\workspace\HelloWorld

Two files should reside here:

   HelloWorld.class
   HelloWorld.java

The *.java file is the text file that you were typing into in Eclipse. The *.class file is the file that Eclipse made when it compiled your HelloWorld.java file. The *.class file contains the object code that is run by the Java virtual machine. (There may be other files here too, but don't worry about those right now.)

Import an existing project

For this exercise, we will use the Fish class from lecture last Thursday, September 12.
  1. First we need to get the Fish project, which has already been created. Go to the following URL, and choose to save the file when prompted. Save the file Fish.zip to your desktop:

    http://www.sims.berkeley.edu/academics/courses/is255/f03/labs/lab091803/Fish.zip

  2. On your desktop, right-click on Fish.zip and choose Extract to folder ...\Fish.
  3. Copy the folder Fish to your eclipse/workspace directory.
  4. Switch to Eclipse and choose File > Import.
  5. Select Existing Project into Workspace and click Next.

  6. Navigate to the Fish folder you just copied into your eclipse directory and click OK, and then click Finish.

    The Fish project has been added to your Package Explorer window.

  7. Expand Fish so you can see the contents, then Expand the default package. Double-click on Fish.java to open it.

    You can run this application (Run > Run As > Java Application), but it won't do anything interesting. That is because it has no output.

  8. Write a new method for the Fish class that prints out the values of a Fish object. Here is the new method's signature:

    public void printValues()

    NOTE that this method takes no parameters and returns nothing.

  9. Call this new method from within main() like this:

    charlie.printValues();

    Now when you run the application, it should print out the values within the charlie object.

Using Textpad

Textpad is a more basic alternative for creating and editing your Java code. It isn't as powerful as the Eclipse IDE and lacks functionality like advanced debugger tools and project/file management, but still a simple and quick way to get simple Java programs written, compiled, and run.

Here is a quick overview of the main features Textpad offers.

The main window of Textpad is where all your code is written and edited.

Some quick configuration:

  1. Select "Preferences" from the "Configure" menu.
  2. In the lefthand tree, double click on "Tools" (or click the + next to it). It should open and display the Java tools available. You should have "Compile Java", "Run Java application", and "Run Java Applet". Click on each one in the left hand explorer menu to get the screen below.

  3. For both "Compile Java" and "Run Java Application", make sure that the "Capture output" option box is checked. This allows alert messages and results from compiling and running your Java code to be printed out into a window for you to see. Leave everything else as is and click OK to exit Preferences.
Okay, you are now ready to try out Textpad with the ubiquitous HelloWorld program.

  1. Type in or cut and paste in your HelloWorld program.
  2. From the File Menu, choose Save As and type in the name "HelloWorld". Select Java(*.java) from the Save as type menu. You can select the directory you would like to save your files in also, note that you should always save your work in your Home (H:/) directory or it can be lost.
  3. Now that you've saved your file as a .java program, Textpad will automatically implement syntax highlighting, defining key Java terms in your code as different colors.
  4. To compile your code, choose "Compile Java" from the "Tools" menu.

  5. You should see below that a new document called "Command Results" has appeared.
  6. Click on it and you should see the message "Tool completed Successfully."
  7. Try going back to your code and removing a parentheses or spelling "System" wrong. Now compile and note that Textpad will alert you to errors and give a list of where it thinks they are. A very useful feature is "Line Numbers" under the "View" menu, as Textpad will reference each error by line number.
  8. Now that you have a successful compile (you'll need to recompile if you've tried altering your code in step 7), select "Run Java Application" from the "Tools" menu.
  9. Click OK if you see the following prompt for parameters.

  10. Yeah! You have successfully run Java program in Textpad.

JDK utilities

There are other utilities that you can use to compile and run your java code. One comes with the Java JDK (Java Development Kit. It has a command line interface and is pretty bare bones, but let's give it a try.
  1. From the Windows Start menu, choose Start > Programs > Accessories > Command Prompt.
  2. Navigate to the HelloWorld folder. To do this, at the prompt, type:

    cd eclipse\workspace\HelloWorld

  3. To see the contents of the directory, type:

    dir

  4. To compile your *.java file, type:

    javac HelloWorld.java

    When the command prompt appears again, this indicates that your program compiled correctly. If there were errors (which there shouldn't be in this example, since you are only re-compiling a program that already compiled in Eclipse) they will be listed

  5. To run the program, type:

    java HelloWorld

    NOTE that you include the *.java extension when compiling with the javac command, but do not include any file extension when running with the java command.

    "Hello World!" should appear, and you will see the command prompt again.

Java Exercises

Pick one (or more!) of the following:
  1. Write the Frog class from homework 2, question 3.(3).
  2. Input and test the code demonstrating variable scope from Thursday 9/12 lecture, slides 32-33.
  3. Write a short Java function that takes a positive integer n and returns the sum of all the positive odd integers smaller than n.

Advanced

Write a java program that displays your course schedule. Your program should include the following:

Note: You should end up with three class files.


JF KS LZ -- last modified 9/18/02